mod_rewrite url with $_GET parameter - php

I honestly can't see where I'm going wrong with mod_rewrite. I'm wanting to build a basic social network, but I want the user profile URLs to look like subdomains, like "www.mysite.com/user/fred" and not "www.mysite.com/user?u=fred". My .htaccess page looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
RewriteRule ^user/([A-Za-z]+)/(0-9]+)/? user?u=$1 [L,QSA]
But navigating to www.mysite.com/user/fred gives me a 404 error. I've tried a few different ones from different tutorial websites and stackoverflow questions, but none of them will work. I know that mod_rewrite is definitely installed on my server, because I checked the php_info() and the rewrite rule for normal URLs work fine ("www.mysite.com/browse" instead of "www.mysite.com/browse.php").

Try replacing your 3rd and 4th line for:
RewriteRule ^user/(.*) user.php?u=$1 [QSA]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Related

.htaccess rewrite for multiple variables

so I have a few different links on my website, and I am looking for some advice on how I can achieve this.
Some example links:
domain/index.php?page=commandCenter&ID=40
domain/index.php?page=view&action=edit&ID=40
domain/index.php?page=acpDashboard
I would like the rewrite to look like the following for each link:
domain/commandCenter/40
domain/view/edit/40
domain/acpDashboard
Though it seems in order for this to work, I need to have something in place for those variables.
The last link would not work, but
domain/acpDashboard/0/0
is what it needs to work.
Is this possible, and how would I go about it? I only have 3 different variables page, action, and ID.
Here is my htaccess atm!
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /index.php? page=$1&action=$2&ID=$3 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thanks for any advice!
Your here is wrong you can reference .htaccess RewriteRule to preserve GET URL parameters this answer
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /index.php?page=$1&action=$2&ID=$3 [L]

.htaccess url rewriting not working?

I'll be honest in saying I have very little experience with .htaccess as I've always wanted to stay away from it as best I can. However, I've recently wanted to tidy up my urls and I've found that it's possible through .htaccess and rewriting.
Basically, I want to rewrite a url like:
www.mysite.com/profile.php?id=48194
To something like:
www.mysite.com/profile/48194
Here's the code I have currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^profile/(.*)/$ profile.php?id=$1
The line I'm trying to use is on the very bottom, RewriteRule ^profile/(.*)/$ profile.php?id=$1. The rest is used to remove the page extensions from the urls. I've changed $1 to $2 thinking perhaps it was conflicting with the code above, but nothing changed.
I also removed all the code except for RewriteEngine on and the last line thinking maybe the codes were conflicting but, again, nothing changed or worked. The rest of the code does work, removing the extensions from urls that is, so I know the rewrite thing is on.
Could someone try to break down and explain what I did wrong and how all this works? As well as providing a working example of the thing I'm trying to accomplish?
Thanks in advance!
Change order of your rules and use MultiViews option. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule to redirect to php file not working

I just want a simple redirect to clean up the url's on a site.
e.g.
I want ajhtestserver.com/registration/ to redirect to ajhtestserver.com/registration.php
It should be easy and I have successfully used .htaccess rewrites on other sites but for some reason it just will not work for me today.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^registration[/]$ registration.php [NC,L] # Handle requests for "registration"
I am sure it is something simple that I am missing but I basically just copied what I have on other sites that work fine for me so I am confused as to why it just refuses to work for me here (gives me The requested URL /ajhtestserver/registration/ was not found on this server. error). Just one of those days :(
Any help is appreciated.
Thanks,
Adam
if you use apache ,first you should enable rewrite_mode in http.conf or ...\
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^registration/(.*)$ registration.php/$1 [L]
check .htaccess syntax or rewrite mode.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)[/]$ $1.php [L]
Well it didn't seem to like it when the redirect source word and target filename were the same word but this works...
RewriteRule ^([a-zA-Z\ ]+)[/]?$ $1.php [NC,L]
And that is actually a better solution anyway as it doesn't require a separate rule for each page.
Though I never did figure out why it didn't like it the original way.

removing .php extension from .htaccess (404?)

I'm trying to removing .php extension from the url using .htaccess i've found a view sites that told me how to do this and i'm using;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However when i go to mydomain.com/user/settings (that would be mydomain.com/user/settings.php without the .htaccess)
It works fine. However when i go to mydomain.com/user/settings/ (trending splash on the end) it gives my custom 404 error page i've looked around on here (& Similar Questions) ask and google but can't really seem to work out what part i'm missing out?
Thank You,
Flexer
Try this to handle the trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]

mod_rewrite for multiple requests not working

Perhaps someone can help me with this before I go completely crazy with it. I have a site with two urls which I want to redirect.
I want all traffic except the gallery pages to go to index.php.
This is my htaccess:
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery(/((([a-zA-Z0-9-]+)(/(\d+))?)/?)?)?$ gallery.php?groupId=$4&showpage=$6 [NC,QSA,L]
This works except for one part. The rewrite rule for the gallery is not fully working. It is sending through the groupId to the gallery.php script but I am not getting through the showpage argument. In fact when the showpage is included in the url I get a 404. So for instance.
These work and are handled correctly by the rewrite rule
gallery
gallery/
gallery/mygroup
gallery/mygroup/
This does not work and throws 404's.
gallery/mygroup/2
Nothing I do seems to fix this and I would appreciate your help on this. If I set the showpage. The gallery script works if I feed it the old
gallery.php?groupId=mygroup&showpage=2
so I am sure the htaccess rule is not catching the url.
Thanks in advance.
Your regex seems a little over-the-top. Use two simpler rewrite rules instead:
RewriteRule ^gallery/([a-zA-Z0-9-]+)/?$ gallery.php?groupId=$1 [NC,QSA,L]
RewriteRule ^gallery/([a-zA-Z0-9-]+)/(\d+)/?$ gallery.php?groupId=$1&showpage=$2 [NC,QSA,L]

Categories