Mode Relay Issues

I have successfully created rewrite rules to handle URLs such as

http://example.com/xyz

http://example.com/xyz/

http://example.com/xyz/abc

http://example.com/xyz/abc/

Here is the mod and regex:

Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?([0-9]+)(?:\/)?$ /index.php?p=$1 [L] RewriteRule ^/?([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1 [L] RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$2 [L] 

This works fine, but the problem is that when I go to http://example.com/xyz/abc my script gets the correct input (n = abc), but the paths are confused, so all the relative URLs in my code broken, because now they refer to xyz instead of the root.

Is there any way around this? As you can see above, I tried redirecting to /index.php to make the path be correct. My brain fries after a long day of regular expression and code, so I hope this is not something catastrophically trivial.

+4
source share
5 answers

Three options:

1) Use a base tag (this will affect every relative URI on your page, including links, images, scripts, style sheets, etc.)

 <base href="http://yoursite/"> 

2) Change all your links to fully qualified URIs

 <a href="http://yoursite/yourpage.html"> 

3) Use the "/" prefix to indicate that the path refers to the root in each URI.

 <a href="/yourpage.html"> 

I personally used the base tag option, which causes some bad printing (from people who used it without understanding it). When it comes to mod_rewrite, the base tag is perfect as you probably want all your paths to be relative to the root, including all your images, css, scripts and links.

+7
source

Use the HTML base to force all relative URLs to use a different path than the one displayed in the browser.

 <base href="http://www.example.com/"/> ... <a href="relative/url">link</a> 

The relative URL will use " http://www.example.com " as its base, even if the browser considers it to be looking at the page " http://www.example.com/xyz/ ". So the link goes to " http://www.example.com/relative/url " instead of http://www.example.com/xyz/relative/url "

And there is a way in which each URL is either an absolute path (to resources, like images), or paths, including a root (for example, "/ feeds / questions / 123") that avoid the relative path of questions.

+8
source

Edit: I assume that you are talking about URLs in your HTML code, for example. to images and style sheets that are broken.

No, as far as I know, this is not so, because the browser sees the path and requests resources in relation to it. It has nothing to do with the server, and you cannot do anything.

You will have to either resort to another splitter that is not interpreted as a directory separator (for example, underscore), or use absolute paths, or use the <& base GT; tag . However, I have never used a base tag, and it is not well seen wherever you look. It would be best to go on absolute paths.

+2
source

change your last RewriteRule to include the path. you have a path in regex.

 RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ $1/index.php?n=$2 [L] 

or alternatively (depending on what you are trying to achieve):

 RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1/$2 [L] 
+1
source

I tried using the BASE element on my pages as a shortcut instead of changing all the urls. Add the base element as follows:

 <base href="/"> 

And here are the results:

 This: <link type="text/css" rel="stylesheet" href="my.css"> Becomes: <link type="text/css" rel="stylesheet" href="/my.css"> This: <script type="text/javascript" language="javascript" src="include/my.js"></script> Becomes: <script type="text/javascript" language="javascript" src="/include/my.js"></script> This: <a href="foo.html"> Becomes: <a href="/foo.html"> This: <a href="foo/bar.html"> Becomes: <a href="/foo/bar.html"> 

You can always override the base tag:

 This: <a href="/foo"> Remains: <a href="/foo"> This: <a href="/foo/bar/"> Remains: <a href="/foo/bar/"> 
+1
source

All Articles