htaccess RewriteCond and RewriteRule - .htaccess

I'm actually working on a little php webiste and i decided to try to give it some nice and cleans urls using a htaccess file. I've read some docs about it and created a simple file with a few rules, things is it's working but partially.
This is what I did:
I added a RewriteRule to redirect "www.mysite.com/hello" to "www.mysite.com/index.php?action=hello".
This is working fine, problem is i want to hide the "index.php?action=" part if it ever appears. So if somoene imputs something like "www.mysite.com/index.php?action=hello" it will be converted to www.mysite.com/hello" in the url field.
For this i wrote the following:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?action=([^\ ]+)
RewriteRule ^index\.php$ /%1? [R=301,L]
Sadly it only hides the index.php part of the url, the "?action=" part is still there.
What did i do wrong?
Regards.
Edit: i'm testing this on local in wamp

You're pretty close. The regex in the rule is trying to match article.php but the condition says that the request must be for index.php, so it's unlikely the rule will ever run. Try:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?action=([^&\ ]+)
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?action=$1 [QSA,L]
and make sure the rule is before the rule that rewrites internally back to index.php.

Related

.htaccess file hide GET request in URL

I am very new to .htaccess files and may have done mine wrong. I have been gathering snippets of code trying to get done what I want.
This is what I have in my file so far
I read this should be in your .htaccess file
Options +MultiViews
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
This was added to force a redirect from HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This was added to remove .php at the end of all the files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This was added to remove the showing of /index when the home link was pressed
RewriteCond %{THE_REQUEST} ^.*\/index?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L]
I have tried a lot of ideas I found on both here and other sites from Google. I have had no luck hiding the GET request.
Currently, the URL looks like https://mysite.ca/event?id=123
What I would like it to look like is either https://mysite.ca/event/123
This is hosted on Godaddy and it is just a plain PHP site if that makes any difference.
Any ideas would be great!
First of all Options +MultiViews already removes .php extension so you don't need:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
About your problem try this Rule:
RewriteRule ^event/([0-9]+)?$ event?id=$1 [NC]
Which changes url from example.com/event/<number> to example.com/event?id=<number>
Although, if your site makes infinite redirects then you should change it to:
RewriteRule ^events/([0-9]+)?$ event?id=$1 [NC]
This happens if you have some includes/headers that redirect back to event and creates infinite loop.

htaccess mod url re-writing

I am rebuilding an old website and have a bunch of old url's that I am having issues re-writing properly.
For example, some of my old URLs are structured as the following:
mydomain.com/?x=about-us **and** mydomain.com/?x=services
I would like the examples to rewrite or redirect to the following:
mydomain.com/about-us **and** mydomain.com/services
Essentially, if there is an occurrence of '?x=' in the URL I would like to strip it out.
Currently the only other cond/rule I have in .htaccess is to remove the '.php'. extensions.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This works well enough, and allows me to write my internal links without the '.php' extension, such as the following:
link
This sends me to 'mydomain.com/about-us', which is what I want. My issue is handling URLs with the '?x='
Any help would be appreciated thank you.
You may use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?x=([^\s&]+)\s [NC]
RewriteRule ^ /%1? [R=301,L,NE]
# add .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ ?x=$1 [L,QSA]

Using Htaccess to Remove Part Of The URL

I need to remove the /design/ from this URL (and any that contain it).
https://www.domain.com/design/subcategory/productname1
At the end, I want it to look like this:
https://www.domain.com/subcategory/productname1
But since that would obvious cause a 404 of you just deleted that part of the URL, I need to populate that second URL with the contents of the first. I apologize if I am not using the correct terms but here is an example of something similar I have on my site.
In my header menu, I have a link that indicates it will take you here:
https://www.domain.com/index.php?route=product/search&tag=shirts
But I wanted it to look like this:
https://www.domain.com/shirts/
So I researched it and this solution worked great:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
Can something similar be done to remove the /design/ part of the URL?
Thanks!
Try these rules:
RewriteEngine On
# remove /design/ from URLs and redirect
RewriteCond %{THE_REQUEST} \s/+design/(\S*)\s [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# internally add design/ to know set of URIs
RewriteRule ^(skulls-bones|characters|abstract|animals|games|geek-nerd|graphic|keep-calm|movies-tv|pop-culture|tattoo|typography)(/.*)?$ design/$1$2 [L,NC]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/design/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]

Is this htaccess rewrite correct?

I have the following in my htaccess file. What I'm trying to do is to make
domain.com/index.php/view/whatever accessable via domain.com/whatever
and also redirect from non www to www.
This works for all urls that have index.php/view in them but now other URLs that don't have index.php/view in them are breaking not working. Ex: domain.com/index.php/site/pages no longer works since it doesn't have index.php/view in it.
I want htaccess to only affect those URLs that have index.php/view in them and not anything else. What do I need to do to fix that?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/view/$1 [L]
RewriteCond %{http_host} ^domain.com [nc]
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
UPDATE. To narrow things down, How can i have both rules like so. i need them both
RewriteRule ^(.*)$ /index.php/view/$1
RewriteRule ^(.*)$ /index.php/site/$1
So if a request is made to something like http://domain.com/index.php/site/pages, you want it to pass through untouched, but if it's something like http://domain.com/whatever, you want it to get rewritten?
The first rule you have there matches everything (except files and directories). You probably want to narrow the RewriteRule with something like:
RewriteRule !^index.php /index.php/view/$1 [L]
HTH
Neal

mod_rewrite - do two redirects - if the first one dosen't match do the second

This is my htaccess file at the moment
RewriteEngine On
# Only redirect if file dosen't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*) /admin.php
RewriteRule ^([^/]) /index.php [L]
I don't have any idea why this doesn't work. I think I've finally grasped mod_rewrite and then it just does something completely unexpected like this.
Basically if the URL is domain.com/admin/something then I want it to redirect to domain.com/admin.php (including if its just /admin). However if its ANYTHING else I want it to redirect to index.php.
Any idea how to achieve this?
Thank you.
RewriteEngine On
# No redirect, if file or directory exists
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^admin/(.*) /admin.php [L]
RewriteRule ^(.*)$ /index.php [L]
Didnt test it. The interesting part is the L-Flag after the admin-rule, because it prevents the next rule from matching.
I changed the RewriteCond-Statements, because they only apply to the one next RewriteRule and (in your case) doesnt affect the rule to index.php.

Resources