I am new on that htaccess thing.
My problem is, I have a pagination on index.php:
http://localhost/index.php?pagina=2
I want to access this with this url:
http://localhost/page/2
I try on htaccess:
RewriteEngine On
RewriteRule /page/(.*)$ index.php?pagina=$1
but it gave me a 404 error.
In per-directory .htaccess files the directory prefix, of where this .htaccess file is located, is removed from the URL-path when pattern matching. In the document root, this is simply / (a slash). So your RewriteRule would need to be rewritten as:
RewriteRule ^page/(.*) /index.php?pagina=$1 [L]
Use:
RewriteEngine On
RewriteRule page/(.*)$ index.php?pagina=$1
without first /
Related
I want to do a simple redirect so that a request for app/scans/large/as89q6dfa.jpg results in app/scans/medium/as89q6dfa.jpg, etc. The trouble is that this app will be used on a few domains with different base paths. The code I've tried keeps rewriting to the absolute base path of the site and the app is actually a variable number of folders deep on the site. Is there a generalized way to do a redirect like this, without hard-coding the base path?
Here's my file and folder scructure:
app/.htaccess
app/scans/large
app/scans/medium
So the .htaccess rules should work for:
includes/app/scans/large
inc/app/scans/large
script/engine/app/scans/large
Here's my first attempt:
RedirectMatch 301 scans/large/(.*) scans/medium//$1
Here's my second attempt:
RewriteEngine On
RewriteBase /
RewriteRule ^scans/large/(.*) scans/medium/$1 [R=301,L]
When you use the "^" caracter it means that the regex will search for the string "scans/large" in the BEGINNING of the URL.
So as the base is "/", it won't work on "includes/app/scans/large/omg" because "scans" it's not in the beginning of the string.
Try this solution:
RewriteEngine On
RewriteBase /
RewriteRule (.*)scans/large/(.*) $1scans/medium/$2 [R=301,L]
It worked perfectly with the URL:
http://www.example.com/includes/app/scans/large/omg
That redirects to:
http://www.example.com/includes/app/scans/medium/omg
You can do more tests on it here: link
You can actually get RewriteBase be determined dynamically.
RewriteEngine On
# determine BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteRule ^scans/large/(.*)$ %{ENV:BASE}scans/medium/$1 [R=301,L,NC]
im doing a cms at the moment
now im struggeling with the ajax implementation
i have everything running except a mod_rewrite problem..
RewriteEngine On
RewriteRule \.html index.php [L]
this redirects nothing except html files to index.php
i need a second rule witch checks the REQUEST_URI for a parameter to prevent the full site gets loaded by ajax.
i dont think this is understandable so i just post what i want to achieve^^
RewriteEngine On
RewriteCond %{REQUEST_URI} !(?rewrite=no$)
RewriteRule \.html index.php [L]
i want nothing redirected except html files and also no redirect on url's with "(.html)?rewrite=no" at the end
hope someone can help me since rewrites and regexp are not my stongest stuff
thanks in advance
From the Apache docs:
REQUEST_URI
The path component of the requested URI, such as "/index.html". This notably excludes the query string which is available as as its own variable named QUERY_STRING.
So you are actually looking to match on %{QUERY_STRING} rather than %{REQUEST_URI}. Don't include the ? on the query string when matching its condition:
RewriteEngine On
# Match the absence of rewrite=no in the query string
RewriteCond %{QUERY_STRING} !rewrite=no [NC]
# Then rewrite .html into index.php
RewriteRule \.html index.php [L]
I have a site with a folder, and a htaccess file within that folder. For the index.php file within that folder, I want to rewrite the querystring for a certain parameter, so that typing in this URL:
www.example.com/myfolder/myparameter
Behaves like this (ie makes $_GET['parameter'] = 'myparameter' in my code)
www.example.com/myfolder/index.php?parameter=myparameter
I have looked at many questions on StackOverflow, but have not managed to get this working. My code so far is
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*) [NC]
RewriteRule ^$ %0 [QSA]
But that just isn't working at all.
Please use this code
RewriteEngine on
RewriteRule (.*) index\.php?parameter=$1 [L,QSA]
RewriteEngine on
RewriteRule (^.*/)([^/]+)$ $1index\.php?parameter=$2 [L,QSA]
update
sorry use #somasundaram's answer. Per-directory .htaccess rewrite rules lose the directory prefix:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.
(from the apache docs)
My htaccess file is under "myappname" folder.
I'm trying to redirect this path;
myappname/CONTROLLER_NAME/ANYFILE.(css|js|gif|jpg|png)
To that path
myappname/views/default/tpl/CONTROLLER_NAME/ANYFILE.(css|js|gif|jpg|png)
and this is my HTACCESS rule
RewriteEngine on
RewriteRule ^(.*)/(.*)\.(css|js|gif|jpg|png)$ views/default/tpl/$1/$2.$3 [L,NC]
but it's giving me HTTP 500 unless I redirect it to a PHP file like
RewriteEngine on
RewriteRule ^(.*)/(.*)\.(css|js|gif|jpg|png)$ views/default/tpl/$1/index.php?a=$2.$3 [L,NC]
What is wrong with my rule? I'm very new to htaccess and there is a very big potential to I'm missing something small.
This is because your target matches the regex:
views/default/tpl/foo/bar.png
matches the regex:
^(.*)/(.*)\.(css|js|gif|jpg|png)$
So the rules just keep looping. You need to add a condition:
RewriteCond %{REQUEST_URI} !^/views/default/tpl
right above your RewriteRule.
I'm currently working with:
RewriteEngine On
RewriteBase /somepath
RewriteRule ^/$ home [R]
I'd like to have requests of GET /somepath to redirect via 302 to /somepath/home. What am I missing here?
When using mod_rewrite in an .htaccess file, the base path is removed from the requested URL path before testing the patterns. That means /somepath (or rather /somepath/, since you’re actually in /somepath/) is removed so that the remaining path is empty. So:
RewriteRule ^$ home [R]