I have some problems with my URL rewriting.
I just want to change my URL paradigm's who is :
http://www.siteadress.com/index.php?//something/something to something like that :
http://www.siteadress.com/something/something
So I add this line :
RewriteRule (.?)index.php\?/(.*) /$1$2
But that does'nt work as I planned.
That just remove 1 / from my URL.
Yet in my meaning, I ask for (.?) anything optionnal index.php\?/ index.php?// (.*) and anything, no?
Thank you for your help.
It looks like you have the rule in reverse order. The first argument should be the pattern you're looking to match, the second argument being what you want to replace it to. In your case, you're looking for "/something/something" and wanting to rewrite that to "/index.php?//something/something". So you need something like:
RewriteRule ^/?(.*) /index.php?//$1
That will take anything after the first / in the path and append it to /index.php?//.
Related
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.
I'm working on a private project, which is basicly a profile system.
Now I've been trying how to improve the URL formatting.
At this moment, I am using URL's which look like:
-> http://example.com/overview
-> http://example.com/profile/?name=foo
-> http://example.com/jobs/?name=foo
Both overview and profile are directories on my website, which contain a single index.php file, which holds the PageID of which should be retrieved from the database.
Now, my goal is to format the URL to something as:
-> http://example.com/foo OR http://example.com/profile/foo
-> http://example.com/foo/jobs OR http://example.com/profile/foo/jobs
Is there anyway to do this with MOD_REWRITE?
This would mean the original url would look something like http://example.com/?character=foo/jobs which is http://example.com/get_var/directory.
I've done my research on Stackoverflow and Google, searching for 'mod_rewrite get variables'. But nothing seemed to be what I'd like to see happening.
Yours Sincerely,
Larssy1
To extract a portion of the URI between slashes and append it as a URL parameter, use the expression ([^/]+) to capture all characters up to but not including the next / into $1:
RewriteEngine On
RewriteRule ^profile/([^/]+)/([^/]+/?)?$ profile/$2?character=$1 [L]
The above rule will dynamically capture what follows Foo from your example, meaning that whether it was followed by /jobs or /other or /somethingelse, it would be appended as /profile/somethingelse?character=Foo. If that isn't necessary, and /jobs is static, you don't need to capture it into $2:
RewriteEngine On
# Don't dynamically capture what comes after the first variable group...
RewriteRule ^profile/([^/]+)/jobs$ profile/jobs?character=$1 [L]
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]
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.
I have a problem with rewrite rule
my link
is www.something/group/group_id/place/groupName
for this
rewriteBase /
RewriteRule ^group/(.*)/(.*)/(.*)$ /group.php?gid=$1 [QSA,NC,L]
somet times my url may come www.something/group/group_id/groupName.
In Both cases I have to rewrite to group.php and I need only groupid. How to write rewrite rule to work in both situation?
Either use lazy quantifiers or prevent each matching group from matching the / itself. The way you have it currently, the first group will match as much as it can resulting in unwanted results.
RewriteRule ^group/(.*?)/(.*?)/(.*?)$ /group.php?gid=$1 [QSA,NC,L]
RewriteRule ^group/([^\/]*)/([^\/]*)/([^\/]*)$ /group.php?gid=$1 [QSA,NC,L]
An even better way, to allow people to leave out unnecessary parts (read: not needed to evaluate the result on the server side), you could even do something like this:
RewriteRule ^group/(\d+)(/.*)?$ /group.php?gid=$1 [QSA,NC,L]
(This is based on the assumption that your group id is a number)
Try this one:
^group/(.+)(/|/.+)*$
It matches for
www.something/group/group_id/place/groupName
www.something/group/group_id/groupName
I never used the RewriteRule, so it's not tested. And maybe if you add the "Regex" Tag to your question you'll get more answers ;-)