htaccess mod url re-writing - .htaccess

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]

Related

Htaccess URL rewrite with get parameters

so I've recently changed my website's file structure a little bit and now need to redirect my old URLs to the new ones.
This is what the old URLs looked like:example.com/script?id=1&title=TitleHere
the script.php is now in a sub-folder with the name "script", so the new URL is:example.com/script/1/TitleHere (This one is already working, but I still need my old urls to redirect to this pattern.)
This is what I tried in my htaccess:RewriteRule ^script?id=([0-9]+)&title=(.*) /script/$2-$1 [L,R=301]
Not sure if this is of importance but I am also using these htaccess settings:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Sadly my attempt didn't bring me any success, it's just throwing a 403 and I think that's because a folder named "script" already exists but I am not sure. Help would be highly appreciated!

Rewrite url for seo friendly htaccess

I want to rewrite this in SEO friendly url.
From - www.example.com/section.php?name=website
To - www.example.com/section/website
I tried this:
RewriteEngine On
RewriteRule ([^/\.]+)/([^/\.]+)?$ section.php?type=$1&service=$2 [NC,L]
I am fetching data using GET method in section.php and pass to the another page but after opening sub folder like www.example.com/{foldername} it returns to the section.php
Thanks. I will appreciate your help.
With your shown samples, please try following htaccess Rules file. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for rewrite to section.php as per OP's request.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ section.php?name=$1 [L]
This helps me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ([^/\.]+)/([^/\.]+)/?$ search.php?type=$1&service=$2

Why doesn't url rewriting work for .html extensions?

I am trying to get
/pages/Settings.html
to appear as
/Settings
by using the RewriteRule
RewriteRule ^([A-Za-z_]+)$ pages/$1.html [NC] # Handle pages
in the .htaccess file. This does not work. However,
/pages/ANYOTHERFILE.php
works by using
RewriteRule ^([A-Za-z_]+)$ pages/$1.php [NC] # Handle pages
to be rewritten into
/ANYOTHERPAGE
Why does the former not work and the latter work? How do I get .html extensions to be rewritten like this?
Add this rules to top of your .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pages/$1.html [L]

.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.

Setting .htaccess to go to https and php instead of html

I have my hosting with 1and1, I have just got a SSL certificate. I currently have changed the .htaccess to load .php files instead of html files which works fine with this code:
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Now I want the site to load with https, regardless of if it is entered like http://www or http:// etc. I am getting so many different ways to do this from various places as most same that I am supplying duplicate sites if I don't specify either http:// or http://www. So I think I will be solving 2 problems in one go. The code 1and1 supply is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.sitename\.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.sitename.co.uk/$1 [R=301,L]
But this does not seem to do much on its own? Can I conbine the 2 to simply load https://www.site for everything and change all links to load .php.
This works perfect
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Resources