HTACCESS directories redirect - php

I'm trying to redirect the address
www.example.com/tops/articles/first_article
to the address
www.example.com/tops/test1.php?name=first_article
also to redirect the address
www.example.com/tops/galleries/first_gallery
to the address
www.example.com/tops/test2.php?name=first_gallery
and all other addresses like
www.example.com/tops/first_page
to
www.example.com/tops/page.php?name=first_page
The following htaccess file is giving me a redirect loop.
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^/tops/articles/(.+)$ /tops/test1.php?name=$1 [L]
RewriteRule ^/tops/galleries/(.+)$ /tops/test2.php?name=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./page.php?name=$1
Doe's anyone has any idea why is that?
What am I doing wrong?
Thanks in advance.

Does page.php exist in the root directory (or any other non /tops/ directory for that matter)?
If not, then that is probably your problem. You should change your last rule to something like:
RewriteRule ^tops/(.*)$ /tops/page.php?name=$1

Well, I finally got it working.
Thought I share it so if anyone every search for this question he will find it.
Here is the code that workd:
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^articles/(.+)$ ./test1.php?name=$1 [L]
RewriteRule ^galleries/(.+)$ ./test2.php?name=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./page.php?name=$1 [L]
Enjoy !

You may try this:
RewriteEngine On
RewriteRule ^tops/articles/([a-zA-Z0-9-=_.]+)/?$ http://www.example.com/tops/test1.php?name=$1 [L]
RewriteRule ^tops/galleries/([a-zA-Z0-9-=_.]+)/?$ http://www.example.com/tops/test2.php?name=$1 [L]
RewriteRule ^tops/([a-zA-Z0-9-=_.]+)/?$ http://www.example.com/page.php?name=$1 [L]
Replace all 3 rules with these rules.
UPDATED for all characters:
RewriteEngine On
RewriteRule ^tops/articles/([^/]*)/?$ http://www.example.com/tops/test1.php?name=$1 [L]
RewriteRule ^tops/galleries/([^/]*)/?$ http://www.example.com/tops/test2.php?name=$1 [L]
RewriteRule ^tops/([^/]*)/?$ http://www.example.com/tops/page.php?name=$1 [L]
.htaccess should be in root directory If it is in /tops directory, try removing it from the pattern and the substitution URL.

Related

htaccess URL's redirect 301 to lowercase with exceptions

I want to change all my URL's with lowercase using RewriteMap lc int:tolower in my Apache config file an in my htaccess have this regular expression:
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]
.. and work purfectly! But i have Libraries (with files) and Directories who i don't want to change to lowercase (for example: "Images" with all sub directories or Libraries: "Functions/Liberties/Image/Resize.php") ... how to make it?
I tried with RewriteRule ^(Images) - [NC,QSA,L] but don't accept it ... any idea how to make this exceptions?
Thank you in advance and sorry for my english!
You can first disable directory listing using -indexes and now, if the requested URI is an existing directory or a file, you can skip the rewrite rule using !-d and !-f params like below:
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]
Thank you all ... i fix it with this:
RewriteRule ^Admin/ - [L]
RewriteRule ^Apps/ - [L]
RewriteRule ^Bank/ - [L]
RewriteRule ^Downloads/ - [L]
RewriteRule ^Functions/ - [L]
RewriteRule ^Images/ - [L]
RewriteRule ^Pages/ - [L]
RewriteRule ^Scripts/ - [L]
RewriteRule ^Styles/ - [L]
... and i put it before:
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]
For me and my case work, i hope it is the right decision. Thank you again!

How to exclude base url from some rewrite rule in htaccess?

Hey everyone I have a rewrite rule in .htaccess file which is
RewriteRule ^([a-zA-Z0-9\-\_]+)?$ product.php?product=$1 [NC,L]
When I go to www.domain.com it apply the above rule and redirect to product.php and when www.domain.com/index.php it works fine. Where I am doing wrong. Thanks
Remove ? in your regex which is making whole pattern optional and exclude directories from this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ product.php?product=$1 [QSA,L]
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /your_project_folder_name/index.php/$1 [L]
</IfModule>

How to change weird PHP URL with htaccess code?

Hoping someone can help me here. In my .htaccess file, I've got a rewrite rule written as:
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
for a URL that would be something like
localhost/wrestling.php?id=something
And now what I'm trying to do is change a URL like
localhost/category.php?id=something
to
localhost/id/category
The thing to note with this scenario is that this time I'm trying to use the id part as the thing after the first "/" and then add the category part after the second "/".
Below is what my entire .htacces file looks like at the moment.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([^/]*)$ /article.php?id=$1 [L]
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
Any thoughts guys?
You can try
RewriteRule ^([^/]*)/category/?$ /category.php?id=$1 [L]
Try this :
RewriteRule ^([^\/]*)/([^\/]*)$ /$2.php?id=$1 [NC,QSA]
You need two captured parameters.
This expression was tested with http://htaccess.madewithlove.be/.

How to correct this rewrite rule?

I have url as
http://www.mydomain.com/levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c. I need to change this url to http://www.mydomain.com/medium.
I am not familiar with rewrite url.
I tried with RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c, but not worked correctly.
Full rewrite rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteRule ^(.*)$ index.php [QSA,L]
Try to change only that line to:
RewriteRule ^medium /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
Or if you don't care if anything is after medium is the following:
RewriteRule ^medium(.*) /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
Your rules are in the wrong order, try changing them to:
RewriteEngine On
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
The conditions need to be applied to the index.php rule, and not the medium rule. RewriteConds are only applied to the immediately following RewriteRule, not all of them.
This rewrite rule worked fine
RewriteRule ^medium.*$ /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c [R=301,L]

htaccess RewriteEngine

I have an url which is http://www.urlbookmarking.com/bookmarks-details.php?bid=55
and I want it to be like
http://www.urlbookmarking.com/bookmark/55
I wrote in my htaccess:
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
But when I go to the first URL the rewrite engine does not apply my rule. Is there any mistake, or conflict somewhere?
My full htaccess file written as follows
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
Please help me.
The line Options +FollowSymLinks is optional if already configured in httpd.conf
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking\.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [R=301, L]
RewriteRule ^bookmark/([0-9]+)$ bookmarks-details.php?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A few things:
RewriteEngine On only needs to called once, though this may not be causing any problems
I also have RewriteBase / after my RewriteEngine On line
My rewrite rule looks like this: RewriteRule ^common/(.*)$ common.php?file=$1 [QSA,L], which tells me that your rule should looke like this RewriteRule ^bookmark/(.*) bookmarks-details.php?bid=$1 [QSA,L]
you should use only one RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1 - put this line after Options +FollowSymLinks
Try again
Do you want you url to be /bid/55 or /bookmark/55? because you have written it as if it is going to be /bid/55...
Anyway, your .htaccess should look more like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^bid/(.*)$ bookmarks-details.php?bid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
...without the multiple RewriteEngine on directives (these don't break anything but are unnecessary), and without the leading forward slash on the bid rewrite rule. Also, put your new rule before the rules that rewrites for non-existent file so it doesn't rewrite your URL before you get a chance to use it, and add a [L] flag to the rule so it doesn't get further modified by the other rules. Also, add the line start/end markers (^/$) to the rule.
You would only use the leading forward slash if you were putting the rules in httpd.conf, you don't use them in .htaccess files.
If you want your urls to be /bookmark/, just replace bid with bookmark.
This should redirect all '/bookmarks-details.php\?bid=(id)' urls with bookmarks ids (that have only numbers) to /bookmark/(id).
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
Once you successfully rewritten the URL, you then need to write a companion rule to process it, like so:
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
If should go between the rule that always adds 'www' to the beginning and the catch all rule, which I placed at the end. All together, it may look like so:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This link may make things clearer: http://corz.org/serv/tricks/htaccess2.php

Categories