how to write a MVC like .htaccess rule? - .htaccess

my goal is to write .htaccess rule "mvc like"
/param1/param2/param3/../paramN -> index.php?param1&param2&param3&...paramN
Excluding for
/css /img /js
ex:
mysite.com/login/john/white->mysite.com/index.php?login&john&white
mysite.com/css/style.css -> mysite.com/css/style.css (NO RULE APPLIED)
mysite.com/img/mypic.jpg -> mysite.com/img/mypic.jpg (NO RULE APPLIED)

If you need to rewrite the URL and not css, js, img files,
Then you can use this htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILNAME} !-f
RewriteCond %{REQUEST_FILNAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ http://example.com/index.php?$1&$2&$3 [QSA,L]
Which will rewrite this example URL.
example.com/index.php?login&john&white
Note that there are no key/value pairs in your query string so that might not come out as you might expect.
You can add aditional rules for longer or shorter URL's.

Related

.htaccess subdomain Rewrite rule is not working

I am making a website builder an I would like to make urls prettier.
The url would be for example:
https://ccc-bb.example.com => https://example.com/project/show/ccc/bb
This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# prevents files starting with dot to be viewed by browser
RewriteRule /\.|^\.(?!well-known/) - [F]
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\-(.*)$ https://example.com/project/show/$1/$2 [L]
When I use above (https://ccc-bb.example.com) it sends me to the subdomain empty folder. The folder has only the .htaccess file.
What am I missing? I've never edited an .htaccess file and Google didn't help me (or I don't know what should I looking for).
Your first rule for dotfiles is okay but would be better the other way around, since the second part can only match the start, but the first can only match in subdirectories.
RewriteRule ^\.(?!well-known/)|/\. - [F]
Your other rule's problem is that you are expecting it to match the subdomain. RewriteRules do not operate on the entire string you see in your browser's address bar, only the path part, and in .htaccess they see even less as the leading directory is stripped off, too. To access the info you want, use a RewriteCond:
RewriteCond %{HTTP_HOST} ^([^-]++)-([^-.]++)\.example\.com$
RewriteRule ^(?!project/show/).* project/show/%1/%2/$0 [L,DPI]
(You don't need to include \.example\.com$ if your main domain contains no hyphens.)

mod_rewrite does not work

I have the following page name
http://example.co.uk/vehicle.php?size=large
and I have written the following rewrite rule so the domain should look as follows
http://example.co.uk/size/large
Here is the rule
RewriteEngine On
RewriteRule ^size/([a-zA-Z0-9]+)/$ vehicle.php?size=$1
I have two problems:
The first is its not working. In fact its not working at all.
The second is if it does work then it will remove the page name which I don't want. I'm struggling to see my mistake in the first place to fix it but I want the domain to look as follows:
http://example.co.uk/vehicle/size/large
If you want to redirect from /vehicle.php URLs to /vehicle/ URLs, then try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*?)=(.*?)$
RewriteRule ^vehicle\.php /vehicle/%1/%2? [R=301,L]
As for displaying contents of nonclean URLs (like /example.php?foo=bar) at clean URLs (like /example/foo/bar), there is a cyclic-redirection issue when trying to use both internal (from a to b) and external (from b to a) redirections at the same time.
If you've decided to switch to clean URLs, then I would recommend you to use /index.php as the only handler for all requests, and use mod_rewrite solely to redirect from old URLs to clean ones. Moreover, I usually perform most of operations with PHP (by parsing $_SERVER['REQUEST_URI'] via PHP), and use mod_rewrite solely to map all requests to index.php file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,QSA]
That's most flexible, straightforward, and portable solution.

htaccess - rewrite URL to make querystring nice

I have a site with a folder, and a htaccess file within that folder. For the index.php file within that folder, I want to rewrite the querystring for a certain parameter, so that typing in this URL:
www.example.com/myfolder/myparameter
Behaves like this (ie makes $_GET['parameter'] = 'myparameter' in my code)
www.example.com/myfolder/index.php?parameter=myparameter
I have looked at many questions on StackOverflow, but have not managed to get this working. My code so far is
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*) [NC]
RewriteRule ^$ %0 [QSA]
But that just isn't working at all.
Please use this code
RewriteEngine on
RewriteRule (.*) index\.php?parameter=$1 [L,QSA]
RewriteEngine on
RewriteRule (^.*/)([^/]+)$ $1index\.php?parameter=$2 [L,QSA]
update
sorry use #somasundaram's answer. Per-directory .htaccess rewrite rules lose the directory prefix:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.
(from the apache docs)

Use htaccess to rewrite subdomain files to a folder

I need to rewrite a files on subdomain to files stored in the folder with name of the subdomain. Only files that start with sitemap should be rewritten. So, if the requests look like this:
http://demo.domain.com/sitemap.xml
http://demo.domain.com/sitemap_more.xml
http://test.domain.com/sitemap.xml
http://test.domain.com/sitemap_alpha.xml
These should rewrite to files:
/content/sitemaps/demo/sitemap.xml
/content/sitemaps/demo/sitemap_more.xml
/content/sitemaps/test/sitemap.xml
/content/sitemaps/test/sitemap_alpha.xml
So, subdomain name is used as a folder in the rewrite-to-path. This should be rewriting NOT redirecting. Also, it would be good to have rules that will work with any domain without need to change domain name everytime.
You can match the subdomain with a condition, then rewrite it with the rule :
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/content/sitemaps/%1/$1 [L]
Maybe you want to add a filter on .xml
Add :
RewriteCond %{REQUEST_FILENAME} \.xml
Edit
For file beginning with sitemap :
RewriteCond %{REQUEST_FILENAME} sitemap.*\..+$
This will match any string that finish with sitemap. and end. => this is a filename not an inner match of directory.

Storing domain files in subfolder

I'm often updating my website through various design iterations, and want to simplify my life by putting each version in its own folder: ie: www.mysite.com/v1.
How can I store all the contents of my in that folder (/v1, /v2, etc) yet have it accessed by simply typing in www.mysite.com.
I don't want just want to redirect the url, I want to remove the v1 from the url entirely.
Assuming you're using Apache, you could use mod_rewrite for this. Simply create a .htaccess file in the root of your public directory with a simple rewrite rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) v2/$1 [L]
</IfModule>
This will rewrite all URL's to the v2 directory. If you update your site to a new version, simply change v2 into something else and all requests will be rewritten to that directory.

Resources