Troubleshooting mod_rewrite in a .htacces with a LightSpeed - .htaccess

I have a couple web pages located at these locations:
Home Page / Index : www.codeliger.com/index.php?page=home
Education : www.codeliger.com/index.php?page=home&filter=1
Skills: www.codeliger.com/index.php?page=home&filter=2
Projects: www.codeliger.com/index.php?page=home&filter=3
Work Experience: www.codeliger.com/index.php?page=home&filter=4
Contact : www.codeliger.com/index.php?page=contact
I am trying to rewrite them to prettier urls:
codeliger.com/home
codeliger.com/education
codeliger.com/skills
codeliger.com/projects
codeliger.com/experience
codeliger.com/contact
I have confirmed that my htaccess file works and mod-rewrite works to google, but I cannot get my syntax working that was specified in multiple tutorials online.
RewriteEngine on
RewriteRule /home /index.php?page=home
RewriteRule /([a-Z]+) /index.php?page=$1
RewriteRule /education /index.php?page=home&filter=1
RewriteRule /skills /index.php?page=home&filter=2
RewriteRule /projects /index.php?page=home&filter=3
RewriteRule /experience /index.php?page=home&filter=4
How can I fix my syntax to rewrite these pages to prettier urls?

The first thing you should probably do is fix your regex. You cannot have a range like [a-Z], you can just do [a-z] and use the [NC] (no case) flag. Also, you want this rule at the very end since it'll match requests for /projects which will make it so the rule further down will never get applied. Then, you want to get rid of all your leading slashes. Lastly, you want a boundary for your regex, otherwise it'll match index.php and cause another error.
So:
RewriteEngine on
RewriteRule ^home /index.php?page=home
RewriteRule ^education /index.php?page=home&filter=1
RewriteRule ^skills /index.php?page=home&filter=2
RewriteRule ^projects /index.php?page=home&filter=3
RewriteRule ^experience /index.php?page=home&filter=4
RewriteRule ^([a-z]+)$ /index.php?page=$1 [NC]

Related

how to replace "?" and "=" sign with "/" in URL - PHP htaccess

i am working on project, which is running XAMPP localhost and PHP MYSQLI,
my question : how i replace "?","=" signs with "/" slash. ?
like, my url is "archive?date=2017-06-02&p=4"
and i want to force it "archive/2017-08-02/4"
i found many codes on stackoverflow and some other sites, but that are not working for me.
if codes are working then, CSS files and GET method doesn't work on my project.
complete code of .htaccess is given below.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^=]*)=([^=]*)=(.*) /$1/$2/$3 [N]
RewriteRule ^([^=]*)=([^=]*)$ $1/$2 [L]
RewriteRule ^home index.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^about about.php [NC,L]
RewriteRule ^article article.php [NC,L]
RewriteRule ^news news.php [NC,L]
RewriteRule ^video videos.php [NC,L]
RewriteRule ^video?vid=([0-9]+) videos.php?q=$1 [NC,L]
RewriteRule ^article?num=([0-9]+) article.php?num=$1 [NC,L]
RewriteRule ^editorial?num=([0-9]+) editorial.php?num=$1 [NC,L]
RewriteRule ^news?news=([0-9]+) news.php?news=$1 [NC,L]
You cannot check against the query string in a rewrite rule. You need rewrite conditions for that:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2?
Demo here: http://htaccess.mwl.be?share=81e85c09-d505-5206-ab14-6c5059107808
If you want to actually redirect just add [R=301,L] to the end of the RewriteRule.
However, looking at the above I suspect you have your script sitting listening at /archive/index.php?data=foo&p=bar but want URLs to be like /archive/date/p, ie pretty.
This is actually a very common misconception about how htaccess URL rewrites work when you first get into them.
RewriteRules will mask or redirect URLs for you but they cannot change the underlying location a script is located at and thus the address used to pass it information.
In other words - you can mask /archive/index.php?data=foo&p=bar as /archive/date/p so that requests made to /archive/date/p resolve to /archive/index.php?data=foo&p=bar, but you cannot make it so that if you enter /archive/index.php?data=foo&p=bar as URL you have the URL change to /archive/date/p while still serving content from /archive/date/p. It has to be either or.
If this all sounds about right my advice would be as follows:
First, put your code into a different file, say /archive/script.php.
Next add the following to your htaccess:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2? [R=301,L]
RewriteRule ^archive/([^/]+)/([^/]+) /archive/script.php?date=$1&p=$2
Note that the first two lines are the same as before, but now there is a new line that looks for the masked URL format of /archive/date/p and sends it off to the actual script, which is handled by the new RewriteRule.
The behaviour of the new rule is demoed here: http://htaccess.mwl.be?share=06751667-f16f-5c13-91eb-dd5cffdc6db3
Hope this makes sense / helps.

htaccess rewrite rules with regexp and inifite redirect

I have a hard time to create rewrite rule for a redirect using part of an old URL for WP. Example:
Old URL:
http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site
or
http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site
New URL:
http://www.example.com/2014/11/07/my-blog-post
New URL should to have only dates and first three elements of a permalink after stripping from dashes.
My solution came after combining answers from here https://stackoverflow.com/a/32852444/1090360 and here https://stackoverflow.com/a/1279758/1090360
Somehow part for replacing underscores with dashes creates infinite redirect and a server freezes. If I will remove part with replacing underscores to dashes all the rest works as should.
Here are my .httaccess rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#replace underscores with dashes
RewriteRule ^(/news/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/news/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301,L,NC]
#redirect to new URL
RewriteRule ^news/index\.php/([^-]+-[^-]+-[^-]+).* /$1 [R=301,L,NC]
#WP standard stuff
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I think, this is an expensive way to replace underscores with dashes. But this works at least in my test environment. The first rule replaces dashes one by one. The second rule then removes the prefix from the requested URL.
RewriteBase /
# replace underscores with dashes
RewriteRule ^(news/index.php/.+?)_(.*) $1-$2 [L]
# strip "news/index.php"
RewriteRule ^news/index.php/(.*) /$1 [R=302,L]
I played a bit more with your original approach using the N|next flag and crashed my server too. Looking into the error.log, it seems this infinite loop is created by Apache by adding a "path info postfix", which enlarges the URL with the original URL. And so it keeps replacing underscores with dashes on and on.
You can prevent this path info postfix with another flag DPI|discardpath, which gives the following rule
RewriteRule ^(news/index.php/.+?)_(.*) $1-$2 [N,DPI]
This seems to work too. Although I must admit, I don't really understand this "path info postfix" thing. There's also an entry in Apache's Bugzilla, Bug 38642: mod_rewrite adds path info postfix after a substitution occured
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

link redirection using htaccess RewriteRule

I have these links in my website:
www.example.org/folder/files.php?file=folder/document.pdf
www.example.org/folder/files.php?force&file=2009.pdf
and I want redirect to :
www.example.org/files/folder/document.pdf
www.example.org/files/2009.pdf
I tried :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
</IfModule>
but doesn't work!
any help?
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
There are two issues with this rule ... first, what you are matching needs to appear first in the rule, then what you are rewriting appears second - you have that backwards.
Once you reverse that, though, you run into the second issue - you can't match query strings in a RewriteRule, you need to match them in a RewriteCond:
To match www.example.org/folder/files.php?force&file=2009.pdf and redirect it to www.example.org/files/2009.pdf you would do:
RewriteCond %{QUERY_STRING} ^force&file=(.*)$ [NC]
RewriteRule ^folder/files.php$ /files/%1 [R=301, L]
The %1 matches what's in the parentheses in the RewriteCond.
Search on google first. The first thing displayed on google for htaccess is htaccess redirect. I think
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html (same line with a space) should work. Go to http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F . Php would also do the work. Just goolgle things before asking them.

htaccess mod rewrite $_GET to variables with slashes

I would like to rewrite URL's with htaccess to better readable URL's and use the $_GET variable in PHP
I sometimes make use of a subdomain so it has to work with and without. Also are the variables not necessary in the url. I take a maximum of 3 variables in the URL
the URL sub.mydomain.com/page/a/1/b/2/c/3 should lead to sub.mydomain.com/page.php?a=1&b=2&c=3 and the url sub.mydomain.com/a/1/b/2/c/3 should lead to sub.mydomain.com/index.php?a=1&b=2&c=3 where $_GET['a'] = 1
I came up with this after searching and trying a lot
RewriteEngine on
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6&$7=$8 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5&$6=$7 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)$ $1.domain.com/$2.php [L,QSA,NC]
but what I get is an not found server error
I'm not that good at this so maybe I oversee something.
Also I would like it to work with and without a slash at the end
Should I make use of RewriteCond and/or set some options?
Thanks in advance.
When using RewriteRule, you don't include the domain name in the line. Also, make sure you turn on the RewriteEngine first. Like this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$ index.php?$1=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4&$5=$6
The first line will rewrite sub.mydomain.com/a/1 to sub.mydomain.com/page.php?a=1, the second rewrites sub.mydomain.com/a/1/b/2 to sub.mydomain.com/page.php?a=1&b=2, and so on.

.htaccess: If any of these directories requested, rewrite to script and pass the requested dir as variariable

I have a bunch of different blogs on my site i.e.
mysite.com/news
mysite.com/blog
mysite.com/bits
etc..
I use the same php script behind the scenes and want to keep the urls tidy so I've setup these rules:
# blogs
RewriteRule ^(bits|blog|news)(/?)$ news/?type=$1
RewriteRule ^(bits|blog|news)/[a-zA-Z0-9_-]+/([0-9]+)(/?)$ news/article.php?type=$1&id=$2
Now what's happening is that the 'type' is always coming through as news? I don't see why.
I would suggest adding a leading '/' to the URL your are redirecting to - also you don't need to wrap the trailing '/' in parentheses.
Also, put a [L] flag at the end of the rule to stop it looking at the rest of the .htaccess
Try this:
RewriteRule ^(bits|blog|news)/?$ /news/?type=$1 [L]
RewriteRule ^(bits|blog|news)/[a-zA-Z0-9_-]+/([0-9]+)/?$ /news/article.php?type=$1&id=$2 [L]
Prepending a RewriteCond of ..
RewriteCond %{QUERY_STRING} ^$
.. to each rule will also filter out any chance of URL's with a query string being parsed.

Resources