Redirecting all subfolders to URL with GET parameters - apache

Quick question for redirecting rules. I'm making a url shortener service and I'd like to have all my links from:
http://www.shortener/url
to :
http://www.shortener/index.php?p=url
I tried on my OVH .htaccess the following code:
RewriteEngine On
RewriteRule ^/(.*)$ index.php?p=$1 [R=301,L]
But this does not work, I tried with removing the "/":
RewriteEngine On
RewriteRule ^/(.*)$ index.php?p=$1 [R=301,L]
But I have a ERR_TOO_MANY_REDIRECTS error on my page.
What should I do?

Why you are seeing error because you have infinite loop occurred there, so we need to place a condition there. Could you please try following, based on your shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.*)/?$ index.php?p=$1 [L]

Related

Apache RewriteRule not working – page not found

I have no idea why it doesn't work, /spelling/30000 gives the Not Found page. Please help.
RewriteEngine On
RewriteBase /
RewriteRule ^/([0-9]+)$ /?mod=spelling&word=$1 [PT]
RewriteRule ^$ /?mod=spelling [PT]
With your shown samples please try following .htaccess rules file. Considering that index.php file is the one which is taking traffic in backend for internal rewrite rules. Please make sure to keep your .htaccess file along side with index.php file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteRule ^/?$ index.php?mod=spelling [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?mod=$1&word=$2 [QSA,L]

htaccess remove folder redirect

I have a problem removing folders from an url. I want that google / old links aren't broken. The old webpage had several sections with a structure like this
example.com/news/items/entry1.html
example.com/news/items/entry2.html
example.com/blog/items/foo.html
The new page has the urls like this:
example.com/news/entry1
example.com/news/entry2
example.com/blog/foo
Removing html was rather straight forward
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php [QSA,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
</IfModule>
The part I'm struggling with is removing the 'items' part. The rules I found only worked for request path like 'example.com/items/subfolder1/...'
Any help would be greatly appreciated.
To remove /items/ from your URLs you can use the following in your .htaccess file:
RewriteEngine On
RewriteRule ^(.*)/items /$1 [L,R=301]
So for example, this will take the URL: http://example.com/news/items/entry1 and turn it into http://example.com/news/entry1
Make sure you clear your cache before testing this.

htaccess is not working after changing server with codeigniter

i am using codeigniter with .htaccess
in older server i had following rules in .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^transaction\.php$ index.php/printer_api/transaction/%1/%2/%3/%4/%5/%6/%7/%8 [L]
RewriteRule ^getlist\.php$ index.php/printer_api/getlist [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
it was working fine.
but after moving the website to a new server the URL .htaccess wasn't working
it was showing "Input file is not specified"
after searching the web i found a solution that is:
changing the Rule in last line from:
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
to (adding extra ? just after index.php):
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
this solved generic URL rewrite with codeigniter.
but still i have problem with following part those are specific for separate URL:
RewriteRule ^transaction\.php$ index.php/printer_api/transaction/%1/%2/%3/%4/%5/%6/%7/%8 [L]
RewriteRule ^getlist\.php$ index.php/printer_api/getlist [L]
in the first URL there are maximum 8 parameters (but the number of parameter can vary in different case
and in the second URL there is no parameter.
but these URLs are not working in new server.
now can anyone please help to make those two Rule working.
Not sure if it'll work, but did you try a similar solution as the one for index.php? Something like:
RewriteRule ^transaction\.php$ index.php?/printer_api/transaction/%1/%2/%3/%4/%5/%6/%7/%8 [L]
RewriteRule ^getlist\.php$ index.php?/printer_api/getlist [L]

htaccess cannot match the whole URL

I am accessing the following URL:
http://example.com/welcome
Where I have the following .htaccess
RewriteEngine On
RewriteRule ^(.*)$ /index.php?package=base&page=$1 [L]
I am expecting to return
package=base
page=welcome
But instead is giving me a "500 Internal Server Error".
If I change the htaccess to
RewriteEngine On
RewriteRule ^we(.*)$ /index.php?package=base&page=$1 [L]
It returns:
package=base
page=lcome
As expected, and if I change the htaccess to
RewriteEngine On
RewriteRule ^(.*)me$ /index.php?package=base&page=$1 [L]
It returns:
package=base
page=welco
As I was expecting as well.
Now the question is... why it does not math the whole URL? What I am missing here? How can I say "take everything the user passes and put it on a variable"?
Thanks!
The rewrite engine loops, so without some sort of conditions, the regex ^(.*)$ matches index.php and so on. Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?package=base&page=$1 [L]

subdomain htaccess

I have here a subdomain which i wish to pass on.
here the example of url : http://subdomain.domain.com/login
and it should point to : http://subdomain.domain.com/index.php/login
i write a simple htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|images|robots\.txt|css|javascript)
RewriteRule ^(.*)$ index.php/$1 [L]
but i always get 500 server error. any body have idea where i wrong?
thanks for any help
It's normal, you go to http://subdomain.domain.com/login, get redirected to http://subdomain.domain.com/index.php/login, then to http://subdomain.domain.com/index.php/index.php/login and so on because you RewriteRule always match.
You can write `RewriteRule ^([^/]*)$ index.php/$1 [L]
Make sure Apache rewrite module is active and your .htaccess file has the following line before any rewrite rule:
RewriteEngine On
Assuming you have mod_rewrite enabled, your RewriteRule causes an infinite redirection loop, which exceeds the maximum number of redirects and causes an internal server error.
You need to condition your rule so it only rewrites once. For example, this should work:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]