Redirect link to new link with anchor tag - apache

I have the url: www.example.com/test/here.
I want to re-direct it to: www.example.com/test2/home#here.
I have tried:
RewriteRule /apply$ www.example.com/test2/home#here [R, NE]
But I am currently getting a server error. Sorry I am not very good with htaccess files.

You are getting a server error because of this:
[R, NE]
You can't have spaces in your rewrite flags, it confuses mod_rewrite and makes it think it's an extra parameter, thus making it think you've not closed your flags with a ].
Besides that, you're regex pattern: /apply$ will never match /test/here. Perhaps, you should try ^test/here$ instead.

Related

Make optional parameter with .htaccess

I have looked for the answer to my problem here, but the solution is always provided without explaining how to do it, that's the reason I can not do it properly.
I have this code:
RewriteRule ^dex/([^_]*)/([^_]*)/([^_]*)/([^_]*)/([^_]*)$ /dex.php?one=$1&two=$2&three=$3&four=$4&five=$5 [L]
This htaccess as it is makes it mandatory that all parameters are given. This URL works:
http://example.com/dex/one/two/three/four/five
I also want to make it work like this:
http://example.com/dex/one/two/three/four
Making the last (or some) parameters optional. I read something about QSA|qsappend here: http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_qsa but I can't understand it completely.
Any help? Thank you
To anyone having the same issue, this is the code used to fix it:
RewriteRule ^dex/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?([^/]*)?$ /dex.php?one=$1&two=$2&three=$3&four=$4&five=$5 [L]
Changed ([^_]*) to ([^/]*) and added ? after what I wanted to make optional.
In this case: /? is making a end slash optional, and ([^/]*)? is making the last parameter optional. So it works when the URL is like this:
http://example.com/dex/one/two/three/four/
http://example.com/dex/one/two/three/four
http://example.com/dex/one/two/three/four/five
http://example.com/dex/one/two/three/four/five/
Hope this helps someone.
Rewrite rules use regular expressions. These regular expressions are always tiresome and difficult to maintain. The following, for example, is an answer to question about regex parsers on HTML, and the difficulty you may find with them:
RegEx match open tags except XHTML self-contained tags
Your htaccess file is an Apache configuration file. It should be used for simple configuration, and not for programming. Have it point to the code, and then let the code do the rest. For example, your .htaccess file:
RewriteRule ^(.*)$ index.php [QSA,NC,L]
And, if you're using PHP with index.php...
$objects = explode('/', ltrim($_SERVER[REDIRECT_URL], '/'));
print_r($objects); // list of items from URL
You may end up wanting to grab a different SERVER parameter, but you'll want to keep this complicated stuff in the code, not in configuration files.

Apache mod_rewrite path name as query parameters?

I want to use Apache's mod_rewrite in order to be able to take each folder of a path as a particular query parameter, for example consider the following:
Basic example
Url requested: http://domain.com/shoes/prada/image-1/
Page served: http://domain.com/?cid=shoes&bid=prada&pid=image-1
In this scenario, there are 3 sub-folders requested (/shoes/, /prada/ then image-1), so the first sub-folder is passed in the actual page served as cid, the second as bid and the third as pid.
Full example
However, I would also like it to serve a particular page depending on the number of sub-folders requested, e.g.
Url requested: http://domain.com/shoes/prada/
Page served: http://domain.com/shop.php?cid=shoes&bid=prada
So far all I've managed to find is regex based matching for mod_rewrite but my path's will vary a lot, which is why I would like to have conditions based on the number of folders accessed (please note, I'm not that good with regex - I reckon a wildcard character would help with this, but I wouldn't be sure where to start).
Any help on this would be greatly appreciated! This is pretty long winded, so if you need any more info for clarifying, please let me know!
With a little bit of work I was able to tweak some regex and get a working rule set for what I wanted:
RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)/(.)?$ product.php?tid=$1&sid=$2&eid=$3 [L]
RewriteRule ^(.*)/(.*)/(.)?$ brand.php?tid=$1&sid=$2 [L]
RewriteRule ^(.*)/(.)?$ shop.php?tid=$1 [L]
This is a bit different to the example, however it's what I intended for in the first place.
This allows for the rewriting of url's up to four folders deep, with the "name" of each folder being given as a parameter, and each additional level of depth rewriting the url to a separate resource for example:
http://x.com/shoes/prada/2011-high-heels/ -> http://x.com/product.php?tid=shoes&sid=prada&eid=2011-high-heels
Tested on http://martinmelin.se/rewrite-rule-tester/

.htaccess rewrite rules changing dynamic url

I am trying to make the url's better for my website for SEO purposes.
Currently pages are displayed like this:
SITE/[number]-[page name].html
I would like to get the pages to display in a format like below.
example 1.
SITE/[page name].html
or
example 2.
SITE/[page name]-[number].html
(I would prefer to leave the page number out if possible, like example 1 but if it must be included I would like it at the end of the url, like the second example above.)
I have the following rule in the .htaccess file.
RewriteRule ^([0-9]+)-(.*)$ site_page.php?page=$1
My biggest problem is that I have tried many ways to get the rewrite to do what I need.
I am a complete newbie when it comes to rewrite.
I have changed the rewrite to the following with no joy.
RewriteRule ^(.*)-([0-9]+)$ site_page.php?page=$1
RewriteRule ^(.*)-([0-9])$ site_page.php?page=$1
RewriteRule ^(.*+)-([0-9]+)$ site_page.php?page=$1
RewriteRule ^([.*]+)-([0-9]+)$ site_page.php?page=$1
The above that I have tried was to keep the page id number, I would preferably like to get rid of the numbers altogether.
I have the following Lines in the setting.php file that controls how the website creates the relevant links. And it displays them as I would like. But I get a 404 error because I think the rewrite rule is not displaying what I expected it to display.
// non seo url
$setup_url['normal']['resource'] = "$url_base/site_page.php?page=[number]";
// seo url
$setup_url['seo']['resource'] = "$url_base/[number]-[name].html";
I have looked at so many articles on mod rewrite rules, and seem to be getting no where. Any help would be greatly appreciated.
Many thanks.
Mark.
Seems that you've forgot the .html extension in rule.
RewriteRule ^(.+)-(\d+)\.html$ site_page.php?$1=$2 [L]

using mod_rewrite with two variables in url

hi ive set up a rewrite and it works perfectly, here it is
RewriteRule ^polls-([0-9]+)\.html$ poll.php?poll_id=$1
This would allow an URL like polls-1.html instead of poll.php?poll_id=1.
My problem is this; i have another variable to view results, this is what i would usually call - poll.php?poll_id=1&showResults=1
How do i set up a rule where i can use some thing like: polls-1/results-1.html ?? Ive searched the net for a while but cant find anything
Add another Rule, like this:
RewriteRule ^polls-([0-9]+)\/results\.html$ poll.php?poll_id=$1&showResults=1
It will redirect polls-1/results.html to poll.php?poll_id=1&showResults=1

How would I use mod_rewrite to turn this query string request in to a clean url?

How can I use mod_rewrite to take http://www.site.com/events?pg=4 and turn it to a clean URL, like so: http://www.site.com/events/4 ?
Thanks for the help!
Something like this should do the job:
RewriteEngine On
RewriteRule ^events/([^/]*)$ /events?pg=$1 [L]
What it's doing is:
Turning on the Rewrite engine
Matching any request that has the form events/something
Storing the 'something' (the brackets indicate that the match should be stored)
Redirecting to the ugly form using the stored variable.
Preventing any further rules from being applied ([L])
Hope this helps.