htaccess Multiple Rewrite issues - php

I'm new to htaccess so this may be easy to many here, but it's eluding me. I have two folders, one is "public" and the other is "admin". The htaccess code was pieced together from elsewhere but it's not properly working for me.
If www.domain.com/admin, then send it to admin/index.php
If www.domain.com/[anything else], send to public/index.php?xxx
If www.domain.com then send to pubic/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^admin /admin/index.php [QSA,L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
This is the htaccess file in the public folder. I do not have an htaccess file in the admin folder.
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
Problems:
www.domain.com/admin generates a 500 Internal Server Error
www.domain.com/anything also generate a 500 error
www.domain.com works as expected
Thanks a bunch for any help!

Use only a single set of rules, do not spread them over several places, that only makes things more complex and error prone. If possible use the real http server configuration instead of dynamic configuration files, more on that below.
RewriteEngine On
RewriteRule ^/?admin/ /admin/index.php [END]
RewriteCond %{REQUEST_URI} ^/public/index\.php$
RewriteRule ^ /public/%{QUERY_STRING} [R=301]
RewriteCond %{REQUEST_URI} ^/public/(.*)$
RewriteRule ^ /public/%1 [R=301]
RewriteRule ^/?(.*)$ public/index.php?$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Related

remove directory name from URL by htaccess from sub directory

I have a root directory where 2 (user & admin) project exist. Now I want to remove directory name from URL only for user. Is it possible?
My directory like
myroot
/admin
/user
What I have tried by htaccess is at /myroot/user/.htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^$ myroot/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!myroot/).+)$ myroot/$1 [L,NC]
But it is not working when I try http://IP/user/Login.php but when I put above htaccess into myroot then it works.
Why I dont want to put this htacces into myroot?
Because My admin link is http://IP/myroot/admin/Login.php is ok but I want to change only user URL from http://IP/myroot/user/Login.php to http://IP/user/Login.php
You need these rules in either a dynamic configuration file (".htaccess" style file) in the http server's DOCUMENT_ROOT folder (which might be myroot here, only you can tell), or, preferably, into the actual http server's host configuration:
RewriteEngine on
RewriteRule ^/?user/(.*)$ /$1 [R=301,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin/
RewriteRule ^/?(.*)$ /user/$1 [END,QSA]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

.htaccess rewrite rule with a fake directory with a parameter

So I was wondering how can I make it where I have a directory for say domain.com/main/ and in that /main I have license.php with .htaccess how am I able to make it where I can do domain.com/main/view/license/(license) and it shows them domain.com/license.php?id={license} ive tried this and it doesn't work
This is my current .htaccess that I use that im trying to make it work with
<IfModule mod_rewrite.c>
RewriteRule ^/view/license/([a-zA-Z0-9\-/\.]+)/?$ license.php?id=$1 [L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]```
Implement this rule in either a dynamic configuration file (".htaccess" style file) in the http server's DOCUMENT_ROOT folder, or, preferably, in the real host configuration:
RewriteEngine on
RewriteRule ^/?main/view/license/?$ /license.php [END]
RewriteRule ^/?main/view/license/([^/]+)/?$ /license.php?id=$1 [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Use
RewriteRule ^main/view/license/([a-zA-Z0-9\-/\.]+)/?$ license.php?id=$1 [L]
Adding main to the rule, and removing the first slash.

Problem with mod rewrite Rules in htaccess

I'm seeting up a new website and want to rewrite some files with parameter in the urls.
The site isn't live right now and unfortunaly i haven't lots of experience in mod_rewrite.
So whats my Problem:
I have two files: category.php and single.php
on my index i have a menu that refers to all categories via url paramater.
For instance on sitename.com/index.php you find links to:
sitename.com/category.php?c=First
sitename.com/category.php?c=Second
sitename.com/category.php?c=Third
and so on
On sitename.com/category.php?c=First for instance you find a list of all posts that refer to category first and linked to:
sitename.com/single.php?c=Frist&name=name1
sitename.com/single.php?c=Frist&name=name2
sitename.com/single.php?c=Second&name=name3
sitename.com/single.php?c=Third&name=name4
and so on
Now i try to rewirte the urls to the following structure:
sitename.com/category.php?c=First => sitename.com/First
sitename.com/category.php?c=Second=> sitename.com/Second
sitename.com/single.php?c=Frist&name=name2 =>sitename.com/First/name2
sitename.com/single.php?c=Second&name=name3 =>sitename.com/Second/name3
I used the following Code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ categroy.php?c=$1 [L,QSA]
RewriteRule ^(.*)/(.*)$ single.php?c=$1&name=$2 [L,QSA]
</IfModule>
Each RewriteRule works for its own but together i will not get the expected results.
so i tried it serval days now and i don't get it.
So hope someone here can help
Thanks a lot
There are several issues here with your attempt, but the main point is that you need to take care about the order of your rules. Since rules are applied from top to bottom you need to place more specialized rules first in the file (so further up), more general rules later ...
Here is a modified version to point you into the right direction:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [END]
RewriteRule ^/?(\w+)/(\w+)/?$ single.php?c=$1&name=$2 [END,QSA]
RewriteRule ^/?(\w+)/?$ category.php?c=$1 [END,QSA]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

.htaccess mod rewrite for product id

Evening guys,
I have been attempting to get mod rewrite working for product id's and name's but I am having no luck at all. I have tried searching here on SO but there are no clear explanations.
This is for my ecommerce demo located http://www.peakwebdesigns.co.uk/ecommerce/store
Example URL: http://www.peakwebdesigns.co.uk/ecommerce/product_page?id=3name=Bed
I want: http://www.peakwebdesigns.co.uk/ecommerce/product_page?id=3name=Bed
to be http://www.peakwebdesigns.co.uk/ecommerce/3-Bed
My URL is structured as follows:
<img src="images/'. $product_array[$key]["image"] . '"></div>' ?>
This is what I currently have in my .htaccess (I know the first few code entries work because I have removed .php and .html from files):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([0-9])-([A-Z]+)$ product_page?id=$1name=$2 [NC,L]
Question: Due to me having /ecommerce/ included in my URL is the above setup correctly?
If not, can anyone help assist me in writing this correctly?
Thanks.
Stan.
There are many possible setups, I expect this to be the one most likely making sense for you (you offered very little information about your situation):
Best is to place such rewriting rules inside the host configuration of your http server:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/ecommerce/(\d+)-(\w+)/?$ /ecommerce/product_page?id=$1name=$2 [END]
Alternatively you can also use a dynamic configuration file. I then would recommend to place that file inside the ecommerce folder and start with something like that:
RewriteEngine on
RewriteBase /ecommerce
RewriteRule ^/?(\d+)-(\w+)/?$ product_page?id=$1name=$2 [END]
For this to work you need to enable the interpretation of dynamic configuration files fist, take a look the the AllowOverride directive in the documentation for that.
Note: in case you receive back an http status 500 (internal server error) with above rules, then chances are that you operate a very old version of the apache http server. In that case try to replace the [END] flag with the [L] flag.
Also, for the sake of precision I want to point out that you need to format the links you send out correctly. You cannot keep the code creating the links unchanged and expect the rewriting module to magically alter the literal links you send out.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
I used the following rules to adjust URLs according to the parameters:
RewriteRule ^ecommerce/product/(.*)$ ./ecommerce/product?id=$1 [L,NC]
RewriteRule ^ecommerce/category/(.*)$ ./ecommerce/category_page?category_name=$1 [L,NC]

How to allow PHP file read parameter passed in a clean way using htaccess

I've been trying fix my error, first here is my .htaccess code:
# Clean Url for User Profiles
AddDefaultCharset UTF-8
Header unset ETag
FileETag None
Options +FollowSymLinks -MultiViews
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+friend\.php\?user_name=([^\s&]+) [NC]
RewriteRule ^ friend/%1? [R=301,L]
RewriteRule ^friend/([^/]+)/?$ friend.php?user_name=$1 [L,QSA]
# End Clean Url Htaccess
My intentions with this code is to make this URL http://localhost:8888/circlepanda/friend?user_name=kendrick
Display like this http://localhost:8888/circlepanda/friend/kendrick and work.
The page opens good, burr the parameter isn't parsed. How do I fix this?
There are a number of issues with your code. I tried to fix what I see, here is my version of what you probably are trying to implement:
Options -MultiViews
RewriteEngine on
# external: /..../friend?username=joe >> /..../friend/joe
RewriteCond %{QUERY_STRING} user_name=([^&]+)
RewriteRule ^friend/?$ friend/%1 [R=301,QSA]
# internal: /friend/joe >> friend.php?user_name=joe
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^friend/([^/]+)/?$ friend.php?user_name=$1 [END,QSA]
Depending on your specific situation it might be that you need to add a RewriteBase. See the official documentation for that.
If you experience an internal server error using above lines chances are that you operate a very old version of the apache http server. In that case you need to replace the [END] flag with the [L] flag.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Categories