mod_rewrite so that first-level subdirectory is a GET variable - .htaccess

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

Related

htaccess - rewrite rule - redirect after url rewrite

This is pretty simple, but can't seem to figure this out.
I have a url /shows/index.php?id=1 that I wanted to appear in the url as /shows/1/index.php.
So I added this to my htaccess:
RewriteRule ^shows/([0-9]*)/index.php$ /shows/index.php?id=$1 [L]
This worked like a charm. I can now navigate to /shows/1/index.php no problem at all. However, I can also still navigate to /shows/index.php?id=1. I wanted that page to automatically redirect to the new url, so I wrote this:
RewriteRule ^shows/index.php?id=([0-9]*)$ /shows/$1/index.php [R=301,L]
...but it doesn't do anything. However, if I do something like:
RewriteRule ^shows/([0-9]*)/0$ /shows/$1/index.php [R=301,L]
It redirects just fine. So that makes me think there is an issue with the first part of the rewrite rule, maybe? I'm not too familiar with this sort of stuff.
Since you want to redirect and rewrite the same request, you need to match against %{THE_REQUEST} variable otherwise you will get an infinite loop error on the redirect
RewriteEngine on
# redirect /shows/index.php?id=1 to /shows/1/index.php
RewriteCond %{THE_REQUEST} /shows/index.php\?id=([^\s]+) [NC]
RewriteRule ^shows/index.php$ /shows/%1/index.php? [NC,L,R]
#rewrite /shows/1/index.php to /shows/index.php?id=1
RewriteRule ^shows/([0-9]*)/index.php$ /shows/index.php?id=$1 [L]
Clear your browser cache before testing this.

how to replace "?" and "=" sign with "/" in URL - PHP htaccess

i am working on project, which is running XAMPP localhost and PHP MYSQLI,
my question : how i replace "?","=" signs with "/" slash. ?
like, my url is "archive?date=2017-06-02&p=4"
and i want to force it "archive/2017-08-02/4"
i found many codes on stackoverflow and some other sites, but that are not working for me.
if codes are working then, CSS files and GET method doesn't work on my project.
complete code of .htaccess is given below.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^=]*)=([^=]*)=(.*) /$1/$2/$3 [N]
RewriteRule ^([^=]*)=([^=]*)$ $1/$2 [L]
RewriteRule ^home index.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^about about.php [NC,L]
RewriteRule ^article article.php [NC,L]
RewriteRule ^news news.php [NC,L]
RewriteRule ^video videos.php [NC,L]
RewriteRule ^video?vid=([0-9]+) videos.php?q=$1 [NC,L]
RewriteRule ^article?num=([0-9]+) article.php?num=$1 [NC,L]
RewriteRule ^editorial?num=([0-9]+) editorial.php?num=$1 [NC,L]
RewriteRule ^news?news=([0-9]+) news.php?news=$1 [NC,L]
You cannot check against the query string in a rewrite rule. You need rewrite conditions for that:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2?
Demo here: http://htaccess.mwl.be?share=81e85c09-d505-5206-ab14-6c5059107808
If you want to actually redirect just add [R=301,L] to the end of the RewriteRule.
However, looking at the above I suspect you have your script sitting listening at /archive/index.php?data=foo&p=bar but want URLs to be like /archive/date/p, ie pretty.
This is actually a very common misconception about how htaccess URL rewrites work when you first get into them.
RewriteRules will mask or redirect URLs for you but they cannot change the underlying location a script is located at and thus the address used to pass it information.
In other words - you can mask /archive/index.php?data=foo&p=bar as /archive/date/p so that requests made to /archive/date/p resolve to /archive/index.php?data=foo&p=bar, but you cannot make it so that if you enter /archive/index.php?data=foo&p=bar as URL you have the URL change to /archive/date/p while still serving content from /archive/date/p. It has to be either or.
If this all sounds about right my advice would be as follows:
First, put your code into a different file, say /archive/script.php.
Next add the following to your htaccess:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2? [R=301,L]
RewriteRule ^archive/([^/]+)/([^/]+) /archive/script.php?date=$1&p=$2
Note that the first two lines are the same as before, but now there is a new line that looks for the masked URL format of /archive/date/p and sends it off to the actual script, which is handled by the new RewriteRule.
The behaviour of the new rule is demoed here: http://htaccess.mwl.be?share=06751667-f16f-5c13-91eb-dd5cffdc6db3
Hope this makes sense / helps.

How to rewrite url for certain pages in htaccess?

I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.
Lets say the url is: http://localhost/mySite/article.php?article_title=test-title
then I would like to show it as http://localhost/mySite/article/test-title
This is my current htcaccess file:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1
But for some reason it is not redirecting/showing the correct url. I am not getting any errors.
EDIT
Trying to ask my question again and explain it better. Let's say the url is
http://localhost/www.example.com/admin/editUser.php?user_id=126
and I would like to rewrite the url like this:
http://localhost/www.example.com/admin/user/126
then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.
This is my htaccess file atm. It is in the root of www.example.com
#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests
Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
it works fine.
Why is the users redirect not working? What am I doing wrong.
Try it like this for your rule for article url in mysite directory.
RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]
you need to mention start ^ and end $ of string.

.htaccess redirect in drupal that excludes node edit page

Currently I have the following htaccess redirect rule that detects any path that looks like
mysite.com/node/xxx and redirects it to a .php script that, in turn, finds the URL of the page and redirects the user to it (e.g. mysite.com/page/page.html)
RewriteCond %{QUERY_STRING} ^(page=(31|1))?$ [NC]
RewriteRule ^node/?$ /? [L,NC,R=301]
RewriteRule ^(../)?node/([0-9]+)$ noderedirect.php?nid=$2 [L]
What I want, however, is that also when user accesses
mysite.com/node
or
mysite.com/node?page=xxx
they get redirected to the main page
BUT
when they access
mysite.com/node/xxx/edit
the rule doesn't get activated.
I tried several options (stopping at the one above) and so far I have this, but it still enables users to access mysite.com/node?page=xxx (all the other conditions are working fine.
Can somebody help?
I just want it to go to that page, and not to do anything. but when it's /node/xxx or /node or /node?page=xxx i want it to redirect to noderedirect.php
If you want it to always go to the noderedirect, then try:
RewriteCond %{THE_REQUEST} \ /+node\?page=([0-9]+)
RewriteRule ^ /node/%1? [L,R=301]
RewriteRule ^node/?$ /noderedirect.php [L]
RewriteRUle ^node/([0-9]+)/? /noderedirect.php?nid=$1 [L]

RewriteRule does not work, while the rest do

My blog's .htaccess is setup in such a way that one page is accessed through multiple URLs, and displays different content depending on which URL is visited.
http://kn3rdmeister.com/category/blog/
http://kn3rdmeister.com/2012/
http://kn3rdmeister.com/2012/07/
all are actually using http://kn3rdmeister.com/blog.php.
The .htaccess file is very handy in the sense that I only need to redirect to one page (pretty much ever) just with different query strings. After a lot messing around with 'em, all of my rules finally work, and I'm dang glad that they do. Well, almost all of them work. The last one does not.
the .htaccess:
RewriteEngine On
RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]
RewriteRule ^category/blog/page/?$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]*)/?$ /category/blog/?pagenum=$1 [L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [L]
The last rule is supposed to redirect to the "permanent link" page for each blog post. Being that each URL is unique, I'm using the post URLs as the unique identifier. Essentially, it is supposed to pass the "url" query string through "blog.php". The PHP script takes over, sees that the "url" query string is set, and then loads the only post with that exact URL in it's row.
The script works, but the redirect doesn't. Going directly to
http://kn3rdmeister.com/blog.php?url=http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
will load the right content. However, going to
http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
doesn't.
Try adding QSA (Query String Append). Also, invert rules so that "deeper" links go on top.
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [QSA,L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [QSA,L]
But, you can't use rewritten links in other rules. So wherever you have category/blog/ replace it with blog.php.
Whilst webarto comments are good advice, your problem is a missing [:
^([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/?$
not
^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$

Resources