Thanks to your help guys I was able to make an .htaccess that rewrites the dynamic links into static one. Now I would like to to extend that solution onto the whole site so that I load accurate module based on $_get variable. Then thanks to .htaccess I should be able to make seo friendly links. The only issue is... it's not working... It shows me an error:
403 Forbidden You don't have permission to access this document.
I am using this script to rewrite the links:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /mod1/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/mod1/index.php\?module=([^\s&]+) [NC]
RewriteRule ^ /mod1/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^mod1/([^\s&]+)?$ mod1/index.php?module=$1 [L,QSA]
Right now I am using the folder mod1 but normally I would like to have there a domain, so that it would have:
www.domain.com/index.php?module=create_list
to be redirected to:
www.domain.com/create_list
I have found an information on the net that I should change the permissions of the file I target (which is index in my case) to 755 but it didn't work in my case. Another mystery of that script is that the first part seems to work, but I am not sure why it always gives the url "/" at the end, even if I did not defined it in the rule.
Any help appreciated.
EDIT:
I manage to fix that problem with mall changes to the script:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /mod1/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/mod1/index.php\?module=([^\s&]+) [NC]
RewriteRule ^ %1.html? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^([^/]+).html?$ index.php?module=$1 [L,QSA]
It is working poperly. However I still do not understand few things. Firstly if I remove the # from:
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
The [L,QSA] still works but the permament [R=302,L] not, I understand that those two line are for files that are used with the php file and they fix the problem with pathways. But how to make them work together w permament redirect 302?
Second thing is I needed to add .html at the end of static link, which was not part of my plan. Is it possible to make it work without .html extension?
If you want to have more than one variable passed from SEO url back to your index.php you could do something like:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?module=$1
RewriteRule ^([^/]+)/([^/]+)$ index.php?module=$1&var2=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?module=$1&var2=$2&var3=$3
Make sure to put that .htaccess file in your mod1/ directory and remove the old one.
If you access /mod1/search
the request will end in index.php?module=search
.
/mod1/search/Books
=> index.php?module=search&var2=Books
mod1/search/Books/20
=> index.php?module=search&var2=Books&var3=20
I don't see any point why you would redirect /mod1/index.php?module=something
to /mod1/something
because this will lead to endless loop:
/mod1/index.php?module=something
=> Server redirects it to /mod1/something
/mod1/something
=> Server redirects it to /mod1/index.php?module=something
and so on... Not to mention the duplicated content you'll end with from the search engine's point of view.
EDIT 1: If you want to make the trailing slash optional, just append /?
at the end of the RewriteRule:
RewriteRule ^([^/]+)/?$ index.php?module=$1
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?module=$1&var2=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?module=$1&var2=$2&var3=$3