mod_rewrite issue with single file no query string - php

Trying to use mod-rewrite to alter the address bar versus the actual page that's being loaded. It works great when I have a query string to use, but I need to do it without a query string, just a straight up pretty url.
It seems to be that without a query string, straight mod_rewrites to files just shows the actual filename with extension.
Here's what my htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
This works:
RewriteRule ^admin/account/([^/]*)/$ /adminconfig/account.php?id=$1 [L]
This doesn't:
RewriteRule ^admin/account/$ /adminconfig/account.php [L]
What's happening is that in the address bar I see:
example.com/adminconfig/account.php/
should be:
example.com/admin/account/

You need to do your rules like this. I also made sure multiviews was off
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/([^/]+)/?$ /adminconfig/$1.php [L]
This rule should allow you to use a URL like this below in the address bar
example.com/admin/account/

Related

PHP redirect to another page with dynamic url and get data [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

Friendly URL profile and about using rewrite in htaccess

I have this url
http://example.net/test
but i have need
http://example.net/test/info
I have this code in my htaccess ,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./id.php?u=$1
how to can i used code with like a link
http://example.net/test/info
Ok. We explain it.
Your htaccess:
Options All -Indexes
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?$1 [L]
Your php:
var_dump(explode('/', explode('?', $_SERVER['REQUEST_URI'])[0]));
You can use this parts for call (with router) to your routines of site. Implementations of controllers, methods, routers - many and different. I do not know what you doing. I only suggest you choose the right way.

htaccess removes the GET variables

I am editing a script and need to read GET variables to make my job done. But it seems the .htaccess file is manipulating it and removes everything at the end of custom URLs. I have no idea how to modify apache configurations to make it to works fine for the script yet me.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?route=$1/$2 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L]
</IfModule>
Here is a sample of URL I need to call:
http://domain.tld/controller/plugin/function/route?k1=v1&k2=v2
and the $_GET only contains one 'route' key with the value below:
controller/plugin/function/route
And other query strings are missed. What should I do for having them?
You probably want the QSA: Query String Append rewrite flag which does what it says on the tin.
For example:
RewriteRule ^(.*)/(.*)$ index.php?route=$1/$2 [L,QSA]

URL rewriting using php not working

I have my url as
http://public_html.com/sub-products.php?catid=42
I want it to look like
http://public_html.com/sub-products/catid/42
I have made a .htaccess file too. but still there is no change in my url
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^sub-products/catid/([0-9]+)/?$ sub-products.php?catid=$1 [L]
Do i need to change anything in my php file also?
what code is required for php file to use .htaccess file?
Your rule in .htaccess rewrite only incoming seo-pretty URls to URL with php script filename. But if you need construct url as /sub-products/catid/42 you have to change you PHP code, that render HTML output.
for example
while ( $products as $product ) {
echo '<a href="/sub-products/catid/"'.$product['id'].'>'.$product['name'].'</a>;
}
Maybe there is a purpose to your line
RewriteRule ^(.*)$ $1.php
But i can't see it for the need you have, so i guess a
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sub-products/catid/([0-9]+)/?$ /sub-products.php?catid=$1 [L]
should work.
Edit :
This should work if you are trying to access the http://public_html.com/sub-products/catid/42 url
(or http://public_html.com/sub-products/catid/42/ btw)
If what you want is going from
http://public_html.com/sub-products.php?catid=42
to
http://public_html.com/sub-products/catid/42/
You might need something like this :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^catid=([0-9]+)$
RewriteRule ^sub-products.php$ /sub-products/catid/%1/? [R=301,L]
And then, you can use what i said before.
I am not sure if you switched your first statment around, but it looks in your .htaccess file you want to rewrite the other way around. Maybe this would work.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} catid=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/catid/%1 [R,L]
If you want to have a more generic rule, and not just for catid this RewriteRule could work:
RewriteCond %{QUERY_STRING} ([a-zA-Z]+)=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/%1/%2 [R,L]
Hope it helps you forward.
EDIT: Made some changes after testing it on my system.

Trying to create tiny urls, getting redirect loop

I'm trying to create tiny urls like this:
site.com/abc123
goes to:
site.com/index.php?token=abc123
but I keep getting redirect loops no matter what I try, or it tries to redirect to index.php?token=index.php..
Current .htaccess is:
Options +FollowSymLinks
Options -MultiViews
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?token=$1 [L]
Here's what I've done (I'm redirecting alphanumeric codes like http://myurl.com/b32ad ):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) /index.php?token=$1 [L]
I have answered a similar question yesterday: htaccess: Redirect a Dynamic URL - Show only Static URL - Double Content
This should do it:
RewriteCond %{QUERY_STRING} ^token=([a-zA-Z0-9]+)$
RewriteRule ^/ /%1? [R=302,L]
That's strange since you have the [L] option attached to that rule. Could there be an external redirect caused by something else?
Anyway, you could limit the rule to requests for non-existing files (maybe directories, too).
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?token=$1 [L]
see http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond

Categories