.htaccess ModReWrite help - php

I am having some trouble with my ReWrite code. Please note that the .htaccess file is in the subdomain folder (...public_html/subdomain/ )
I am simply trying to rewrite a page request:
http://subdomain.mysite.com/home
http://subdomain.mysite.com/index.php?page=home
My .htaccess file looks like this...
RewriteEngine On
RewriteRule ^/([A-Za-z0-9\-\_])$ /index.php?page=$1
Does anything jump out at you?

Your current rule probably works for urls one character long (after the slash)!
Add a + to signify one or more characters, or a * for zero or more
Try
RewriteEngine On
RewriteRule ^/([A-Za-z0-9\-\_]*)$ /index.php?page=$1

If you want to use the rules in a .htaccess file, you need to strip the contextual per-directory path prefix from the RewriteRule pattern. If the .htaccess file is located in the document root /, you need to strip the leading /.
Additionally you need to quantify the character set. Otherwise it would only describe one character.
So try this rule:
RewriteRule ^([A-Za-z0-9-_]+)$ index.php?page=$1

I think
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
is ok ;)

Related

Set .htaccess rules to specify different folders

I want to set a rule in .htaccess if I enter in the url www.mydomain.com/compare.php set 'public_html' as root otherwise anything come in the url set root as 'public' folder.
RewriteRule ^(?!compare-source.php).*)$ public/$1 [L]
I want to achieve following result.
if url is www.mydomain.com/compare.php hit following file.
public_html/compare.php
if urls are www.mydomain.com/ OR www.mydomain.com/home etc hit following file.
public_html/public/index.php
I am weak in regex and in these apache rules always :-( can someone give me the solution with good description?
Your answers are welcome, please can you describe how this crazy things work in detail. Thanks.
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^((?!compare\.php).*)$ /public/$1 [L]
The RewriteEngine directive enables or disables the runtime rewriting engine.
The RewriteCond directive defines a rule condition. The following Rule is only used if this condition is met; In our case, if REQUEST_URI (the path component of the requested URL) does not (because of !) begin (because of ^) with /public. We need this condition because we don't want to rewrite already rewritten URL - that would cause loop and Internal error 500.
Finally, the RewriteRule will match regex Pattern (^((?!compare\.php).*)$) against part of the URL after the hostname and port, and without the leading slash. If the pattern is matched, the Substitution (public/$1) will replace the original URL-path.
In plain language, if URL path does not begin with compare.php (because of ?!), pick everything (.*) between beginning (^) and end ($) and place it in variable $1. Then replace the original URL path with /public/$1.
#Anubhava's answer is also correct, he just placed both conditions in RewriteRule, and also it could be written even more readable as:
RewriteCond %{REQUEST_URI} !^/public
RewriteCond %{REQUEST_URI} !^/compare\.php
RewriteRule ^(.*)$ /public/$1 [L]
You can use this .htaccess in site root:
RewriteEngine On
# route /home/ or /home to /
RewriteRule ^home/?$ / [L,NC]
# if not compare-source.php or public/* then route to /public/*
RewriteRule ^(?!public/|compare-source\.php$).*)$ public/$1 [L,NC]

htaccess not working for directory redirecting

I have directory my-site.com/m/ and it contain so many sub-folder so i want internal redirect for sub-folder to my-site.com/m/req="rest-path-of-directory"
ex
my-site.com/m/article/pc to my-site.com/m/?req=article/pc
For this I use the code below but something is wrong, it is not working for me.
My .htaccess file is :
RewriteEngine On
RewriteRule ^/m/([A-Za-z0-9\/]*)/?$ http://www.localhost/m/index.php?req=$1 [L]
Try
RewriteEngine On
RewriteRule ^/m/([A-Za-z0-9\/]*)$ http://www.localhost/m/index.php?req=$1 [L]
I think the /? outside your capture group brackets is never being matched, as all the / characters are being captured inside your ([A-Za-z0-9\/]*) expression.
For internal redirection you need to remove the hostname and scheme from the Rewrite target
Try :
RewriteEngine On
RewriteRule ^m/([A-Za-z0-9\/]*)/?$ /m/index.php?req=$1 [NC,QSA,L]

RewriteRule for only .php files

I need to rewrite this url:
mysyte.com/index.php?lang=it
to:
mysite.com/it/index
without rewriting css href, javascript src and image src.
I tried many solutions but none worked, could someone help me?
thanks!
edit:
I tried:
RewriteEngine On
RewriteRule ^(.*)/(.*) $2.php?lang=$1
but this overwrites my css calls too.
then I tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*) $2.php?lang=$1
with the same result
EDIT :
In your .htaccess, write the following lines :
# Activate url rewriting
RewriteEngine on
# Rewrite all url of the form /2-letters-language-code/index to index.php/lang=2-letters-language-code
RewriteRule ^([a-z]{2})/index$ /index.php?lang=$1 [QSA,L]
The ([a-z]{2}) part of the regexp means "every group of two letters", so it will catch "it", but also "fr", "en"... and every other language code in two letters. If this is too general, you can replace it with just (it|en|fr) according to your needs.
And if you need to rewrite not just index to index.php, but whatever alphanumeric string, you can do :
RewriteRule ^([a-z]{2})/([a-zA-Z0-9_]+)$ /$2.php?lang=$1 [QSA,L]
Attention to not be too large in the second parenthesis, otherwise the rule will catch strings you don't want. For exemple,[a-zA-Z0-9_]+ means : every group of 1 or more alphanumeric character and underscores. It excludes slashes (/) and hyphens (-).
This was already answered here :
CSS not loading after redirect with htaccess rewrite rule
the easiest way to fix this is to specify the full path to your CSS file in the <head> of the HTML file.
If your mod_rewrite rule is modifying the path to where your CSS file is, you'd also want to place this before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
This makes sure that the request doesn't match a file before rewriting it.

Trouble mapping multiple subfolders to destinations in htaccess

We've switched servers and for whatever reason, our htaccess file isn't behaving the same way as it was on the other. I'm going to be the first to admit that I'm not an htaccess superuser, and I have no doubt the answer's probably looking me in the face, but literally an entire Saturday of searches hasn't fixed this. I have this filesystem:
When the domain is /category/subcategory/product/
It should rewrite to /category/product-details.php?p=product&s=subcategory
When the domain is /category/subcategory/
It should rewrite to /category-product-list.php?slug=subcategory
This seems simple enough, here's the same code we had been using for years. Note, we've commented out the first two RewriteRules regarding slashes and there was no change in behavior.
RewriteEngine On
RewriteBase /
# if folder does not end with a slash redirect to with slash
RewriteRule ^([-a-zA-Z0-9]+)$ /$1/ [L,NC,R=301]
#if it does not end with a slash e.g. rock-jewelry/some-piece, add the slash
RewriteRule ^([-a-zA-Z0-9]+/[-a-zA-Z0-9]+)$ /$1/ [L,NC,R=301]
RewriteRule ^category/([^/]+)/([^/]+)/?$ category/product-details.php?p=$2s=$1 [L,QSA]
RewriteRule ^category/([^/]+)/?$ category-product-list.php?slug=$1 [L,QSA]
When the domain is /category/subcategory/product/
It rewrites to /category-product-list.php?slug=product-details.php&p=product&s=subcategory
When the domain is /category/subcategory/
It correctly rewrites to /category-product-list.php?slug=subcategory
/category/ is an actual folder, and the only thing in it is product-details.php, there is no index.php file, the htaccess is supposed to rewrite to category-product-list if they're trying to access the index.
If we remove the category-product-list.php rule, the product-details.php rule DOES work. But isn't the L directive supposed to stop at the first rule? Why is the second still running? And how can I write a better way to accomplish this goal? Thank you very much, I'm pretty beat down on this problem at this point.
I have the same problem as you to understand the why of this loop... But it's like that.
You can add that before the last RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
Or if you don't use dot in subcategory, you can use final RewriteRule:
RewriteRule ^category/([^./]+)/?$ category-product-list.php?slug=$1 [L,QSA]

Search and replace part of URL via apache htaccess RewriteRule

I'd like to rewrite URLS like:
http://post.local/web/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
to
http://post.local/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
Means the part "web/" should be replaced by ""(or remove it), but I have problems to build a rewrite rule for my .htaccess which cover that.
Note: mod_rewrite is on (RewriteEngine on)
RewriteRule ^web/bundles/(.*) bundles/$1 [QSA]
This should work:
RewriteEngine On
RewriteRule ^web/bundles/(.*) /bundles/$1 [R]
The above will remove web/ from the url so
http://post.local/web/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
will become
http://post.local/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en

Categories