.htaccess rewrite in subdirectories - php

I have wamp server installed and i can not rewrite my urls from a subdirectory
http://localhost/new1/articles/details.php?name=test
to
http://localhost/new1/articles/test
i tried like this
RewriteEngine On
RewriteRule ^new1/articles/(.*)/$ /new1/articles/details.php?name=$1 [L,R=301]
and this
RewriteCond /articles/ -d
RewriteRule ^/articles/([^/]*) /articles/details.php?name=$1
Nothing works...i get 404 not found.
what am i doing wrong?

Place this rule in /new1/.htaccess:
RewriteEngine On
RewriteBase /new1/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^articles/([^/]+)/?$ articles/details.php?name=$1 [L,QSA,NC]

Related

vanity url with .htaccess in php

I'm trying to create a vanity url for where users profile could easily be viewed by others.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://localhost/test/public/profile.php?username=$1 [NC]
This is the code but each time i use this code in my .htaccess file, i get 500 Internal Server Error and the sub-folder public disappears from my directory. What am i getting wrong? Do i need any to do any configuration first?
It's better to use global rewrite logic:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
and in Your index.php catch $_SERVER['REQUEST_URI'] and if user exists so respond if not then pass processing to next procedure.
But if You insist on realization as in question so try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?username=$1 [L,QSA]
Check this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^profile/([a-zA-Z0-9_\-/]+)$ test/public/profile.php?username=$1 [L,NC]
</IfModule>
However note:
You must have the rewrite engine on to make it work (google a bit for the installation procedure the OS/system you are working)
Put proper RewriteBase directive if you use it from subdirectory.

mod_rewrite and apache clean hyperlinks

i've a php website and using mod_rewrite for clean urls.
my files:
villa.php about.php contact.php index.php 404.php
my villa.php is dynamic and works with mysql db. url example:
domain.com/villa?i=2&s=villaname
i want to use it like domain.com/villa/id/villaname
my .htaccess file looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,NC]
RewriteRule ^yazlik/(.*)/(.*)$ yazlik.php?i=$1&s=$2[L,NC]
but its not working. what is the right configuration for .htaccess file? any help would be great. thanks for your time.
You can use this rule in your htaccess file
RewriteRule ^villa/([0-9]*)/(.*)/?$ /villa?i=$1&s=$2 [QSA,NC,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,NC]
RewriteRule ^yazlik/(.*)/(.*)$ yazlik.php?i=$1&s=$2[L,NC]
RewriteRule ^villa/([0-9]*)/(.*)/?$ /villa?i=$1&s=$2 [QSA,NC,L]

Creating a Pretty URL

It almost is similar to how to create pretty urls but, it does not seem to work for me.
Here is my url:
http://localhost/pr/ajax/ajax_load.php?task=get_blob&blid=199
How do I convert it to:
http://localhost/pr/ajax/get_blob/199
Here is my .htaccess:
RewriteEngine On
RewriteBase /pr/ajax/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+pr/ajax/ajax_load\.php\?task=([&\s]+)&blid=([0-9]+) [NC]
RewriteRule ^/%1/%2? [R=301,L,NE]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ /pr/ajax/ajax_load.php?task=$1&blid=$2 [L,NC,QSA,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|gif|png|pdf)$ /pr/ajax/%{REQUEST_URI} [NC,L,R=302]
Where should the .htaccess be placed? I mean, does it matter where you put it at? My root is at pr.
The location of the .htaccess file is important, specially for the URI-path tested in the rewrite rule, that contains only the path segment after the directory where the file is located. To overcome that limitation, only the REQUEST_URI variable is used in the following code.
Here is a tested solution that works with .htaccess file at either /pr or /pr/ajax/ directories:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /pr/ajax/ajax_load\.php\?task=([^&]+)&blid=([^&\s]+) [NC]
RewriteRule .* /pr/ajax/%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !ajax_load\.php [NC]
RewriteCond %{REQUEST_URI} ^/pr/ajax/([^/]+)/([^/]+)/? [NC]
RewriteRule .* /pr/ajax/ajax_load.php?task=%1&blid=%2 [L,NC]
Try this:
RewriteEngine On
RewriteRule ^/pr/ajax/(.*)/(.*)$ /pr/ajax/ajax_load.php?task=$1&blid=$2 [NC,L]
And yes, it does matter where you put the htaccess file. You put the file in the directory of the file whose path you want to modify/manipulate.

TinyMCE conflicting with htaccess

I have configured my server using htaccess so that I can route all URL's into a PHP $_GET parameter. I want the URLs on the site to finish with .htm. Currently I have this htaccess, and I have never had any problems with it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ajax/([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1&opformat=ajax [QSA]
RewriteRule ^([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1 [QSA]
However I have just installed TinyMCE which uses .htm files in the js folder e.g. "/js/tinymce/prop.htm". For some reason when this URL is entered my htaccess routes it to php and it doesn't work.
I was given to understand that RewriteCond %{REQUEST_FILENAME} !-f should stop this from happening. Why is it not? RewriteCond %{REQUEST_FILENAME} !-d is working as I can go to "/js/tinymce" in this example and see directory listing.
Thanks.
I suggest you to change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^ajax/([^.]*)\.htm$ index.php?route=$1&opformat=ajax [L,QSA,NC]
RewriteRule ^([^.]*)\.htm$ index.php?route=$1 [L,QSA,NC]

How can I use mod_rewrite to remove the query string from my URL?

I am new at mod_rewrite and need some help if is possible.
this is my old url
www.mysite.com/web/page.php?c=categoryname
I need to change it at:
www.mysite.com/web/page/categoryname.html
I am using this code but it doesn't work:
RewriteEngine on
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ page.php?c=$1 [L]
How can I fix the above code to correctly rewrite my URLs?
EDIT:
Thanks to Ulrich Palha
i manage to redirect the html files, but css,js etc are not redirect and the website looks rly bad, this is the code i am using now, till i will get the one who will redirect css too
RewriteEngine On
RewriteBase /web/
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
BUMP ANY HELP PLEASE?
Add the following to the .htaccess file in the web directory of your site, replacing the rules you have above.
RewriteEngine On
RewriteBase /web/
#do not process css/js etc
RewriteCond %{REQUEST_URI} \.(css|js|jpg|png) [NC]
#alternatively you could skip all existing files. Comment previous line and uncomment next to do so
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
Your regex in the rule is wrong — you are missing /page, adding an optional final slash that you shouldn't be, and forgetting to remove the .html part. Also your RewriteBase shouldn't have a final slash and your rule shouldn't have a leading slash.
It total, it should be this:
RewriteEngine on
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]+)\.html$ page.php?c=$1 [L]

Categories