Url Mod-Rewrite Get new Page with ID - .htaccess

i need some help from the experts.
I have some rewrite rules:
post.php go to http://example.com/post/3/
RewriteRule ^post/([A-Za-z0-9-]+)/?$ post.php?id=$1 [NC,L]
It Works!
But, How can i add a new rewrite rule to the NEW Page Users.php after post/ID/
And get the ID in Users.php (Users.php?id=3)
Like this http://example.com/post/3/users
Thanks!

If you want /post/3/users/ to go to /Users.php?id=3, you have to put that rule before your existing rule. Your existing rule matches /post/3/' which is a prefix of what this additional rule matches, so that rule will never fire if it is after.
# catch the longer URL first
RewriteRule ^post/([A-Za-z0-9-]+)/users/?$ Users.php?id=$1 [NC,L]
# No /users/ on it; rewrite to post.php
RewriteRule ^post/([A-Za-z0-9-]+)/?$ post.php?id=$1 [NC,L]
Another thing: you tagged your post with .htaccess. Does that mean your rewrites are in a .htaccess? If so, you should use RewriteBase because your rewrites are relative. Why it's working is probably that you are allowing a 1:1 correspondence between paths and URLs on your webserver.
In a per-directory context like .htaccess, mod_rewrite is working with path names, not URLs. But if you do a relative rewrite, the path is turned into a URL and fed back into the Apache's request handling chain to be processed over again. How the path is turned into a URL is that the contents of RewriteBase are added to the front. If you don't have RewriteBase then a silly thing happens: the path to your directory (that was removed for the RewriteRule is just tacked back on!).
Example: suppose your DocumentRoot is /var/www. Suppose the browser asks for the URL /foo. This gets translated to the path /var/www/foo If inside the .htaccess for /var/www/ you rewrite foo to bar (and RewriteBase is not set), then mod_rewrite will generate the URL /var/www/bar: it just takes the /var/www/ directory that was stripped off and puts it back on. Now that can be made to work: just make /var/www/ a valid URL going to that directory. For instance with
Alias /var/www /var/www # map /var/www URL to /var/www directory
But that is hacky. The right way is to have RewriteBase / in the .htaccess. So when foo is rewritten to bar, it just gets a / in front and becomes the url /bar. This is fed back to the server and will resolve back to the document root again "naturally".
I used hacks like that before I understood the rewriting. I even used / as a DocumentRoot! That made everything work out since then most URLs are paths: you don't have to think of URLs a an abstraction separate from your filesystem paths. But it's a dangerous, silly thing.

Related

Htaccess and redirect all content of folder, but exlude the main folder

I got the following urls:
domain.com/categoryA/articleA
domain.com/categoryA/articleB
I want to redirect:
domain.com/categoryA/articleA -> domain.com/categoryB/articleA
domain.com/categoryA/articleB -> domain.com/categoryB/articleB
but leave it as it is and do not redirect the main folder: domain.com/categoryA/
I tried to use the rule:
RewriteRule ^categoryA/(.*)$ /categoryB/$1 [R=301,NC,L]
but it also redirect domain.com/categoryA/ to domain.com/categoryB/
How to exclude from the above rewrite rule the redirection of the main folder (categoryA), but still redirect all that is in the folder (and then change also the root folder)?
I am looking for a solution that is SEO friendly (I got the same articles in two categories, but want still to have indexed domain.com/categoryA, but the rest only as domain.com/categoryB/xxx.
Best Greetings,
Mat
With your shown samples/attempts, please try following Rules in your .htaccess file. Please make sure to place this rule under your domain redirect rule(if its there), also make sure to clear your browser cache before testing your URLs.
RewriteRule ^categoryA/([\w-]+)/?$ /categoryB/$1 [R=301,NC,L]

htaccess - redirect a directory including the index page

I am updating a website and having updated a directory name, I need to update all links for the old directory named blog to the newly named press
I have tried this:
RewriteRule ^blog/(.*)$ /press/$1 [R=301,NC,L]
Which works perfect for any url's within that directory. i.e
website-url.com/blog/post-1
Goes too
website-url.com/press/post-1
However the blog index page still doesn't. I instead get a 404 when going to:
website-url.com/blog
If I have a trailing slash, it does work. Just not without.
I know I can use an absolute path redirect like so:
Redirect 301 /blog http://www.website-url.com/press
But the domain could change so I want to keep the path relative / dynamic for this.
Have your rule like this:
RewriteRule ^blog(/.*)?$ /press$1 [R=301,NC,L]
This will match /blog also as /.* part is optional match.

htaccess Rewrite rule to send variable info to script but display clean url

What I'd like to do is when a URL like
http://localhost/sandbox/jcsearch/country/countryname
is typed by the user I would like to to point to
http://localhost/sandbox/jcsearch/index.php?country=countryname
but still retain the original clean URL in the address bar ie
http://localhost/sandbox/jcsearch/country/countryname
Is this possible? would it create any kind of redirect loop?
The rewrite would happen as follows:
RewriteEngine on
RewriteRule ^sandbox/jcsearch/([^/]+)/([^/]+)$ sandbox/jcsearch/index.php?$1=$2 [L,NC]
Since, the RewriteRule directive does not have the redirection flag (R) set, it will not change the URL in your browser's addressbar. So, by visiting
http://localhost/sandbox/jcsearch/country/countryname
user will get internally redirected to:
http://localhost/sandbox/jcsearch/index.php?country=countryname
Please note: You have to put the rewrite rules in the htaccess file in your server root directory.

Simple and neat .htaccess redirect help required

This is a strange one...
A while back I managed to write a .htaccess redirect that worked so that the URL was read like: www.website.com/mt?page=index - and what the real URL of this page was www.website.com/PageParser.php?file=index.php
The problem has been that the FTP system of my webhost hides .htaccess files even though they are allowed and do operate - and so I have checked back on local copies I have of my .htaccess files and none of them have the code as to how this works - and I've forgotten how I did it!!
Essentially, I am using wildcards so that anything after mt?page= will actually be showing PageParser.php?file= but without having the PageParser.php showing within the URL (and this is the important bit, because the index.php on my site root is actually sent through PageParser.php first so that anything which shouldn't be there is wiped out before the end user sees it) - so how can .htaccess redirect/rewrite the URL so that any link to /mt?page= show the file located at /PageParser.php?file= without changing the URL the user sees?
RewriteEngine On
RewriteRule ^(.*)mt?page=(.*)$ $1PageParser.php?file=$2
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^mt$ /PageParser.php?file=%1.php [NC,L]
This rule will rewrite (internal redirect) request for /mt?page=hello to /PageParser.php?file=hello.php without changing URL in browser.
Your source URL example (www.website.com/mt?page=index) has index while target URL (www.website.com/PageParser.php?file=index.php) has index.php. The above rule will add .php to the page name value, so if you request /mt?page=hello.php it will be rewritten to /PageParser.php?file=hello.php.php.
If there is a typo in your URL example and page value should be passed as is, then remove .php bit from rewrite rule.
The rule will work fine even if some other parameters are present (e.g. /mt?page=hello&name=Pinky) but those extra parameters will not be passed to rewritten URL. If needed -- add QSA flag to rewrite rule.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
P.S.
Better write no explanation (I knew it/I did it before .. but now I forgot how I did it) than having these "excuses". While it may be 100% true, it just does not sound that great.

htaccess redirect rules - How to redirect up one level?

http://mysite.com/level-1/level-2/level-3/
I want to redirect to
http://mysite.com/level-1/level-2/
"level-1" and "level-2" can be anything the user enters... (not these exact words)
Could you direct me to a tutorial or give me a few pointers?
Thanks a lot!!
Based on your comment (from level-3 to level-2 folder EXACTLY with 301 Permanent Redirect):
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+/[^/]+/)[^/]+/$ http://www.example.com/$1 [QSA,R=301,L]
This rule will redirect example.com/hello/pink/kitten/ to example.com/hello/pink/
If URL structure is different, then NO redirect will occur:
missing trailing slash (e.g. example.com/hello/pink/kitten)
4-level deep URL (e.g. example.com/hello/pink/kitten/family/)
This rule needs to be placed in .htaccess in website root folder. If placed elsewhere (e.g. Apache config file, inside <VirtualHost>, for example) the rule needs small tweaking.
You just want to throw away what's after the second slash. This is an easy rule:
RewriteRule ^(.*?/.*?/).+$ $1
Then investigate whether you want things like [L,QSA]. You may or may not.

Resources