RewriteRule: Making an exception - remove one argument - php

I'm sorry for this "bad" title, I'll try to explain it:
My .htaccess looks like this:
Options -MultiViews
RewriteEngine On
RewriteBase /mythbusters/tsp/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# RewriteRule ^album/(.+)$ index.php?url=album/show/$1
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
The last line of this file is working perfectly. It allows to rewrite my URL from _ROOT_/index.php?url=album/index to _ROOT_/album/index (I'm using this to work with those parameters and to create beautiful URL's.
Although, there's one exception I want to make:
The out-commented line in my .htaccess file is one of my uncountable tries to rewrite a specific URL.
With the last line of my .htaccess file, the working URL looks like this:
_ROOT_/album/show/name_of_album
which is the same as
_ROOT_/index.php?url=album/show/name_of_album
Now, I want to remove the "show" part of this URL, I tried to to it with
# RewriteRule ^album/(.+)$ index.php?url=album/show/$1
But using this, every other path in my site changes which causes the CSS files fail loading (just pure HTML, no styles).
Are there some missing flags or even some wrong regex in this file?
Thanks for your help.
Chris

Have it like this:
Options -MultiViews
RewriteEngine On
RewriteBase /mythbusters/tsp/
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^album/(.+)$ index.php?url=album/show/$1 [L,QSA]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
For css/js/image issue: use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can also try adding this in your page's HTML header: <base href="/" />

Related

Apache: .htaccess: SEO: how to eliminate query string syntax (PHP)

Excellent help for my question is provided in this post. However, despite trying things as described therein I am still having problems because it seems as though my .htaccess file is not being parsed.
I have a website which consists of the following pages:
http://mywebsite.com/?menu1=home
http://mywebsite.com/?menu1=posts&menu2=johndoe
http://mywebsite.com/?menu1=posts&menu2=janedoe
http://mywebsite.com/?menu1=posts&menu2=nickdoe
http://mywebsite.com/?menu1=fashion&menu2=armani
http://mywebsite.com/?menu1=fashion&menu2=gucci
http://mywebsite.com/?menu1=about
The pages are displayed through the default index.php file in my xampp directory on Windows.
What would it take to have the browser respond to the following SEO frienly links instead? I would like to have my PHP code work with at least modifications as possible.
http://mywebsite.com/home
http://mywebsite.com/posts/johndoe
http://mywebsite.com/posts/janedoe
http://mywebsite.com/posts/nickdoe
http://mywebsite.com/fashion/armani
http://mywebsite.com/fashion/gucci
http://mywebsite.com/about
Thanks.
You can use these rules in your Root/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /?menu1=$1&menu2=$2 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /?menu1=$1 [QSA,NC,L]
The first rule rewrites "/posts/jondoe/" to "/?menu=posts&menu=jonde" and the second rule rewrites "/about" to "/?home=about"..

htaccess url rewriting, ignoring files

I am trying to come up with a rewrite rule, but I am having problems.
What I need - any url that starts with /services/XXX to be redirected to /services/api.php?service=XXX
I also want to ignore any files or folders that might also match.
What I have so far:
RewriteRule ^services/([a-z]+)$ /services/api.php?service=$1 [NC, L]
But this does not work at all, it shows a 404 page saying that file is missing when I test it.
Any help is much appreciated.
To ignore the files that exists just ignore the "RewriteCond"s.
Just rewrite everything that comes into services/ to api.php?service ...
Put this into your services folder.
Be careful with your encoding, save the file as .htaccess with a encode that your server reads.
# .htaccess mod_rewrite
RewriteEngine On
RewriteRule ^(.*)$ api.php?service=$1 [QSA,L]
If you don't need to avoid Files, then just go to:
# .htaccess mod_rewrite
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ api.php?service=$1 [QSA,L]

htaccess add .html extension for urls with or without trailing slash

So to begin with I have a custom url rewrite that sends a request variable to a php script
Rewrite rule is below:
RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]
So if you access something like domain.com/slug-text it sends slug-text to index.php located in folder named test.
What I want is all my urls to look like domain.com/slug-text.html, but slug-test variable should still be sent to index.php file.
And
What I can't figure out is the redirect. I want all the old urls to be redirected from domain.com/slug-text or domain.com/slug-text/ to domain.com/slug-text.html and slug-text sent to index.php file located in test folder.
Searched a lot but could not find the answer for this question anywhere on the Internet.
Thank you all for the help.
UPDATE:
my new code is:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/\-]+)?[\w-])(?!:\.html)$ http://domain.com/$1\.html [L,R=301]
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
domain.com/slug-text/ does not get redirected to domain.com/slug-text.html
domain.com/slug-text works as intended redirecting to domain.com/slug-text.html
What do i need to change?
This rule:
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
Will trap domain.com/slug-text, domain.com/slug-text/ and domain.com/slug-text.html and send slug-text to /test/index.php inside slug param.
If you really want to redirect using [R=301] from old urls to new then use this:
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
Also note that as using explicit redirect bottom rule is modified to trap url's ending with .html
It is also advisable (if your .htaccess does not already contain this) to filter conditions for existing files and folders not to be trapped by your redirect rules. Simply add these lines before RewriteRule lines:
# existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# existing folder
RewriteCond %{SCRIPT_FILENAME} !-d
And if using symlinks:
# enable symlinks
Options +FollowSymLinks
# existing symlink
RewriteCond %{SCRIPT_FILENAME} !-l
// addition
Your .htaccess file should look like this:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
This is supposed to redirect /slug-text to /slug-text.html
RedirectMatch ^/([\w-]+)/?$ http://domein.com/$1.html
This is in the case when slug-text is only letters, digits, – and _.
Rewrite slug-text.html to a php file and pass the slug as a param:
RewriteRule ^([\w-]+)\.html$ test/index.php?slug=$1 [R,L]
If you have both line in your .htaccess the first one will do the redirects from the legacy URLs to the new ones and the second one will process the request.

How to rewrite URL to include variables and remove php while allowing directories

I have the following htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?user=$1
which works as expected for example
http://example.com/index.php?user=USERNAME -> http://example.com/USERNAME
However I have created a form on the page index.php which posts to /directory/save.php
How do I remove .php while allowing for the directory so that I can post to /directory/save/ instead?
if it is going to one and only such file in /directory then probably hard code it by adding following before the above rules?
RewriteRule ^directory/save$ /directory/save.php [L]
directory/ might have it's own rewrite rules and not have a physical save.php, that's why !-f might not work. Try adding a new rewrite condition to stop rewriting for directory/
RewriteCond %{REQUEST_URI} !^directory
Try to check this one if it's rewriting /directory/save.php file to directory /directory/save/
RewriteCond %{REQUEST_URI} ^/directory/save\.php$
RewriteRule ^(.+) /directory/save/ [NC]

How can I redirect every URL that's not requesting an JPEG or PNG image to index.php/x?

For example, I have an URL that looks for an image like this:
http://example.com/img/foo.png
http://example.com/img/interface/menu/bar.png
http://example.com/static/users/avatars/small/3k5jd355swrx221.jpg
I don't want to redirect those. They should just pass through. But then, I have URLs like this:
http://example.com/register/
http://example.com/my_account/my_picture/
http://example.com/contact/email/
All such URLs that don't request for an .png or .jpeg should be redirected to:
http://example.com/index.php/x
Where x stands for everything after example.com/, so in this example for example:
http://example.com/register/ to
http://example.com/index.php/register/
http://example.com/my_account/my_picture/ to
http://example.com/index.php/my_account/my_picture/
http://example.com/contact/email/ to
http://example.com/index.php/contact/email/
(AcceptPathInfo is enabled)
Is there any way to do that in the .htaccess? I only know how I could do this if I had always something like http://example.com/someKindOfMarkerHere/stuff/stuff/stuff but I don't want to have the someKindOfMarker there to detect if it's an URL that has to be rewritten. I don't know how to exclude them.
You can either exclude specific URLs:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule !.*\.(jpeg|png)$ index.php%{REQUEST_URI}
Or you exclude any existing file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^.+\.png$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.+\.jp(e)?g$
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php/%{REQUEST_URI} [NC,L]
Hell yes it's possible.
mod_rewrite will do all of that for you pretty easily.
You can also set up an error handler, so every 404 on your site gets redirected through index.php. This is a nice little way of making sure all requests load index.php (or your bootstrap).
The mod_rewrite will need a regex and regex's hurt my head, so I'll let somebody else write one.
Hope that helps. Just comment if you need more info from me. :)
Put something like this in a .htaccess file and make sure mod_rewrite is enabled:
RewriteEngine On
RewriteRule ^(.*?)(?!(\.png|\.jpg))$ index.php/$1
http://www.regular-expressions.info/lookaround.html
I would use a slight variation to Gumbo's answer:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
It excludes folders as well as files (the !-d flag) - you may not may not want this, but think it belongs here for completeness.
The following ignores existing files, folder and files with the extension matching: jpg, jpeg, png, gif. If you wish to add additional extension, just add "|extension" before the )$ on line 3.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !.(jpg|jpeg|png|gif)$
RewriteRule ^(.*)?$ /index.php/$1 [QSA,L]

Categories