mod_rewrite rewriting a url - apache

Hay, can someone lend a helping hand to get a rewrite rule to work?
I'm developing a CMS and the URL currently look like this
page.php?id=2/About-us
I want to remove the
page.php?id=2/
part of the URL and just show
About-us
Any ideas how to get this working?
EDIT
I have since changed my URLS to
page/PAGE_NAME
and used the rule
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
However, apache just says that index.php was not found on the server.

Are you sure it is index.php and not page.php?
Please try
RewriteRule ^/page/([^/]+)$ /index.php?page=$1 [L]
If you need a permanent move
RewriteRule ^/page/([^/]+)$ /index.php?page=$1 [R=301,L]

Related

.htaccess rewrite for category with pagination

I'm trying to write a mod-rewrite rule to handle pagination links on my site.
I'd like my URL structure to be this https://example.com/category.php?name=category-link to https://example.com/category/category-link [without pagination]
and https://example.com/category.php?name=category-link&page=2 to https://example.com/category/category-link/2 [with pagination]
I've tried the following:
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+) category.php?name=$1&page=$2
https://example.com/category/category-link isn't working
https://example.com/category/category-link/1 is working
2. am i able to redirect localhost/article/4/ to localhost/article/4 [here 4 is an id]
Any guidance would be much appreciated.
Could you please try following, based on your shown samples only. Please make sure you clear your browser cache after placing these rules into your htaccess file.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/category/([0-9a-zA-Z-]+)/?$ [NC]
RewriteRule ^(.*)$ category.php?name=%1 [L]
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+)/? category.php?name=$1&page=$2 [L]
OR you could use it without RewriteCond too. Make sure you are putting either of these NOT both of them please.
RewriteEngine ON
RewriteRule ^category/([0-9a-zA-Z-]+)/?$ category.php?name=$1 [L]
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+)/? category.php?name=$1&page=$2 [L]

Rewrite in HTACCESS

Can anyone help me here? I did a rewrite for the english version of my site.
It's was mysite.com/?lang=en And now it's mysite.com/en/
I've tried redirecting the URL with this :
RewriteRule ?lang=en$ /en/ [R=301,L]
But each time I put this line in my HTACCESS, the website goes down.
Your rewrite rule is trying to use the parameter portion of the URL. There is an existing SO answer with an example:
HTAccess Rewrite based upon a Parameter Value, ignore other Parameters
Perhaps something like this will help:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)(lang=en+).+ [NC]
RewriteRule ^en$ %{REQUEST_URI}?%1 [R=302,NE,NC,L]

mod_rewrite forwarding always forwarding to index page

I have some basic URL forwarding set up in my .htaccess file to create seo friendly links. My problem is that after I added the first two rules any url that begins xxxxxx.com/cm/en forwards to the index page. xxxxxx.com/cm/en/about-condominium-calle-margarita-santa-ana gets forwarded to cm/en_index. I'm sure there is something basic I'm missing here, any help appreciated.
RewriteRule ^cm/en cm/en_index.php [L]
RewriteRule ^cm/es cm/index.php [L]
RewriteRule ^cm/en/about-condominium-calle-margarita-santa-ana cm/en_about.php [L]
RewriteRule ^cm/es/nuestro-apartamentos-calle-margarita-santa-ana cm/es_about.php [L]
The answer for me was to add a $ to the end of ^cm/en.
So I used:
RewriteRule ^cm/en$ cm/en_index.php [L]
RewriteRule ^cm/es$ cm/index.php [L]
I'm guessing the $ means that for that rule to match there can be no more text after the cm/en or cm/es. I'm sure someone else can give a proper explanation!

Why is my simple rewrite code causing a redirect loop?

Would greatly appreciate it if someone could help me make this work.
I'm trying to make domain.com/painting.php?name=hello redirect to domain.com/page/hello while keeping my rewrite :
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^painting\.php$ /page/%1? [R=301,L] #redirects to page
RewriteRule ^page/([^/\.]+)/?$ painting.php?name=$1 [L] #rewrites painting
I would like to keep only the "pretty url". Please help.
Found answer here: simple 301 redirect with variable not working, why?
I'm assuming what you actually want to do is be able to accept the URL domain.com/page/hello and rewrite it (invisibly) to domain.com/painting.php?name=hello. If so, try this
RewriteRule ^page/([^/.])+/?$ painting.php?name=$1 [QSA,L]

RewriteRule is redirecting rather than rewriting

I have a rewrite rule that looks like this:
RewriteEngine On
RewriteRule ^$ store [L]
That's the only thing in the .htaccess file.
It's supposed to allow someone to go to http://www.site.com/ and according to the server, they're accessing http://www.site.com/store .
But it's redirecting the user. In other words, you see "/store" in the URL. How do I avoid this?
By the way, there's not a redirect going on within /store/index.php (the index script in the store directory. I know this because I put a die statement in there and the "/store" is in the URL when the script dies on that script.
For some reason, I changed it from
RewriteRule ^$ store [L]
to
RewriteRule ^$ /store/ [L]
and it started working