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!
Related
I am trying to work on my "pretty-urls" and I'll be the first to admit I really don't know what I'm doing. I'm trying to wrap my head around it though. So what I am trying to do is have a file.php, but then also have a directory called file. That way I can go to www.example.com/file and it should pull up file.php. But I also want to be able to go to www.example.com/file/subfile and it should go to subfile.php IN the file directory.
Right now, I'm able to get rid of ".php" on all files and I've figured out how to "hide" the $_GET parameters in the URL, but this directory/file issue is throwing me for a loop. Any help is appreciated!
This is my current .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^dire1/file/([0-9]+) dir1/file.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^dir2/example/([0-9]+) dir2/example.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
This is because of mod-dir Apache directory module that runs before mod-rewrite . When you request an existent directory without a traling slash the mod-dir issues a permanent 301 Redirect from /dir to /dir/ and that is why you are unable to rewrite the uri.
To fix this, you need to tell mod-dir not to perform the redirection when a directory without a traling slash is requested. You can use DirectorySlash directive in your htaccess to change this behaviour.
Add the following line to your htaccess :
DirectorySlash off
Reference :
https://httpd.apache.org/docs/2.4/mod/mod_dir.html
Have it this way:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^dire1/file/([0-9]+) dir1/file.php?id=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^dir2/example/([0-9]+) dir2/example.php?id=$1 [NC,QSA,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Last rule will serve /file.php when URI is example.com/file/
Note that if you serve example.com/file (no trailing slash) then Apache's mod_dir module will add a trailing slash in the end to make it example.com/file/ and then server /file.php.
Turning off this trailing slash behavior is considered a security risk and it may expose your internal directory structure to your visitors.
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.
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]
I have my htaccess and it already does couple of good things (it hides www and .php in my url) but I also need to add one additional feature to it.
I have the main domain called: wdd.ie
I have a root folder called: wdd.ie
Most important files are kept in the root.
I have also the following file in the root: about.php
I love to access about.php file by calling non existing directory: www.wdd.ie/myweb/
And my full URL path looks like this: www.wdd.ie/myweb/about
I would like to access the file (stored in the root) to allow non existing directory in url but to ignore it
Could anyone help!? Please, please help!
As this new desired htaccess feature complexity is now probably beyond my skills.
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
If I've understood correctly you want the following url www.wdd.ie/myweb/about to offer up content from www.wdd.ie/about.php but keep the url the same?
If so, then the following should work
RewriteRule ^myweb/about$ /about.php [L]
On my server I am using this .htaccess to remove the .PHP extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
And it works fine, /about goes to the about.php page and /contact to the .contact.php file. However I have a projects.php and a projects folder so the hyperlink redirects to the folder index. I understand I could simply rename either the php or folder and solve the problem, but if I have subfolders in the projects folders, I won't be able to keep the URL as /projects/photography
Here is an image to help explain: http://i.stack.imgur.com/TKjEF.png
Try
DirectorySlash Off
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/projects$ [NC]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It should work with DirectorySlash off, but see caveats. If you don't like them you are better off renaming projects.php