Htacces RewriteRule - .htaccess

I currently have this code in my .htaccess working fine:
(ex. RewriteRule "^new-jersey/(.*)" "location.php?c=$1" [NC,L])
This can be accessed at /new-jersey/city_example
However, if I add a simple -second-version at the end, it no longer works:
(ex. RewriteRule "^new-jersey/(.*)-second-version" "location.php?c=$1" [NC,L])
My goal is to access this at /new-jersey/city_example-second-version

Related

I want to use a RewriteRule in .htaccess to allow subfolders

We have a url mydomain.com/events that needs to include a country code just before th events folder like this mycomain.com/uk/events. The country could be any number of different codes. I want to use a RewriteRule like:
RewriteRule ^([a-zA-Z0-9-/]+)/events/*.* /events/*.*
so any any url like these will work:
mydomain.com/uk/events
mydomain.com/uk/events/index.php
mydomain.com/uk/events/list-events.php?s=12
With your shown samples/attempts, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Place your htaccess file along with your domain folder(not inside it beside it).
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([^/]*)/(?:[^/]*)/([^/]*)(/.*?\.php(?:\?[^=]+=\d+)?)?\s [NC]
RewriteRule ^ %1/%2%3 [L]

.htaccess RewriteRule conditional rule

I'm trying to write a rule where the same link will point to the same page, except it will change the variable based on what follows "blog/" part of the URL. For example, if it's:
mycooldomain.net/blog/new
It should redirect to
mycooldomain.net/blog_page/index.php?do=new
I've tried the following but am not getting the desired results:
RewriteRule ^blog/(^guest|new)?$ blog_page/index.php?do=$1 [NC,L]
RewriteRule ^blog/?([a-zA-Z0-9-/]+)?/?$ blog_page/index.php?article=$1 [NC,L]
On my index.php page I will add logic to load different page elements based on $_GET['do'].
What am I missing?
this will do.
RewriteRule ^blog/new$ /blog_page/index.php?do=new [L,R=301]

RewriteRule redirects to unexpected page

I am trying to change my url through .htaccess in the following way
Original URL: http://www.example.com/latest-news.php?id=2/topic=testing
Rewritten URL:http://www.example.com/2/testing
Rule for .htaccess
RewriteRule ^([^/]*)/([^/]*)$ /latest-news.php?id=$1&topic=$2 [L]
This is working fine but the other files which are existing in a folder are not opening. The url is opening as www.example.com/testing/foo.php but content of the page is of http://www.example.com/2/testing
Can you try with a similar rule like the one bellow?
RewriteRule ^([0-9]+)/([a-zA-Z])$ /latest-news.php?id=$1&topic=$2 [L]
Can't really test it on my server but you can adapt it to suit your needs
RewriteEngine On
RewriteRule ^([0-9]+)/([^/]*)$ /latest-news.php?id=$1&url=$2 [L]
Since second part consists characters and digits So I modify the answer of stoica. Thanks #stoica

Jump to Anchor using Mod-Rewrite

I designed my Website as OnePage Design. However, I want to keep using my old URLs. To navigate within my new Website I'm using anchors.
I do not get Mod-Rewrite working using anchors.
The URLs I want to rewrite
www.mywebsite.com/nameOfAnchor
www.mywebsite.com/nameOfAnchor/
To be:
www.mywebsite.com/index.php?#nameOfAnchor
I don't get this working using the following code (Only calls index.php but does not navigate to the anchor)
RewriteCond %{REQUEST_URI} /[^.]+[^/](|/)$
RewriteRule ^(.*)$ /index.php?#$1 [NE]
The rule is working well if I want to hand over a variable via get
RewriteCond %{REQUEST_URI} /[^.]+[^/](|/)$
RewriteRule ^(.*)$ /index.php?variable=$1 [NE]
But this does not solve my problem. Any suggestions?
You need external redirects.
RewriteRule ^(?!index\.php$)[^/]+/?$ /#$0 [NS,NE,B,L,R=301]

htaccess redirection for download.php?fid=##

Is there a way to redirect this :
http://example.com/download.php?fid=##
to another url?
I tried to use Redirect 301 in htaccess but it is not working for me.
Basically, I have an script , where you click on a button and you download a file, that is stored in your website, in your root folder, but I want to store the file externally, for example in Dropbox. If you copy the hyperlink of the button, you get http://example.com/download.php?fid=## --- where ## is query string that identifies the file.
If I remove the ?fid=## part, it works, but then all the downloads will be redirected to the same file.
lets say i have
http://domain.com/download.php?fid=40
I've tried this
RewriteCond %{QUERY_STRING} id=40`
RewriteRule ^sub-dir/download\.php$ /https://dl.dropboxusercontent.com/s/c1x0u1m64i1i186/00431.doc?dl=0/? [L,R=301]`
and Ive tried
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/download.php$
RewriteCond %{QUERY_STRING} ^fid=40$
RewriteRule ^(.*)$ https://dl.dropboxusercontent.com/s/c7x0u1m14i1i186/00431.doc?dl=0 [R=301,L]
It seems the second block of code its working for me now

Resources